Javascript String padRight(ch, n)

Description

Javascript String padRight(ch, n)


String.prototype.padRight = function(ch, n) {
  return this.length >= n
    ? this.valueOf()/*w w w.  j  a  v  a2  s .c  o  m*/
    : (this + Array(n + 1).join(ch)).slice(0, n);
};

Javascript String padRight(ch, n)

String.prototype.padRight = function(ch, n) {
  if(this.length < n) {
    var diff = n - this.length;
    var tempArr = this.split('');
    var tmp;/* w w  w .ja  v  a  2 s  . c o  m*/
    for(var i = 0; i < diff; i++) {
      tempArr.push(ch);
    }
    tmp = tempArr.toString().replace(/,/g, '');
    return tmp;
  } else if(parseInt(this).toString() !== 'NaN'){
    return parseInt(this);
  } else {
    return this.replace(/' '/g, '');
  }
};



PreviousNext

Related