Nodejs String Padding Right padRight(ch, n)

Here you can find the source of padRight(ch, n)

Method Source Code

String.prototype.padRight = function(ch, n) {
  if(this.length < n) {
    var diff = n - this.length;
    var tempArr = this.split('');
    var tmp;/*from  w w  w.j  av  a  2s . 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, '');
  }
};

Related

  1. padRight(count, ch)
    String.prototype.padRight = function(count, ch) {
        var char = ch || ' ';
        var result = new Array(count + 1).join(char);
        return this + result;
    };
    
  2. padRight(count, character)
    String.prototype.padRight = function (count, character) {
        if (typeof (character) === "undefined") character = " ";
        return this.length <= count ? this + Array(count + 1 - this.length).join(character) : this.toString();
    
  3. padRight(count, character)
    String.prototype.padRight = function(count, character) {
        var result = "" + this;
        if (character === undefined) {
            character = " ";
        count -= this.length;
        if (count < 0) {
            count = 0;
        var i;
        for (i = 0; i < count; i += 1) {
            result += character;
        return result;
    };
    
  4. padRight(count, character)
    String.prototype.padRight = function (count, character) {
        if (typeof (character) === "undefined") {
            character = " ";
        return this.length <= count ? this + Array(count + 1 - this.length).join(character) : this.toString();
    };