Javascript String lpad(padString, length)

Description

Javascript String lpad(padString, length)


// Pad the left side of a string with a given character
String.prototype.lpad = function(padString, length) {
    var str = this;
    while (str.length < length)
        str = padString + str;/*from w w w.  j  a  v a 2 s .  c o  m*/
    return str;
};

Javascript String lpad(padString, length)

//pads left//from  w  ww  .  j a v  a2  s  . c o  m
String.prototype.lpad = function(padString, length) {
 var str = this;
    while (str.length < length){
        str = padString + str;
    }
    return str;
}

//pads right
String.prototype.rpad = function(padString, length) {
 var str = this;
 //alert(str.length+'-'+length)
    while (str.length < length){
        str = str + padString;
        //alert(str)
    }
    return str;
}



PreviousNext

Related