Nodejs String Padding Left padLeft(n, ch)

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

Method Source Code

String.prototype.padLeft = function(n, ch){
    var s = this;
    while(s.length < n){
        s = ch + s;/*from w ww  .j  a  v  a  2s .  c o m*/
    }
    return s;
}

Related

  1. padLeft(count, character)
    String.prototype.padLeft = function (count, character) {
        if (typeof (character) === "undefined") {
            character = " ";
        return this.length <= count ? Array(count + 1 - this.length).join(character) + this : this.toString();
    };
    
  2. padLeft(count, character)
    String.prototype.padLeft = function(count, character) {
      var ch = character || ' ';
      var result = '';
      for (var i = 0; i < count; i++) {
        result += ch;
      };
      result += this;
      return result;
    
  3. padLeft(count, character)
    String.prototype.padLeft = function(count, character) {
        character = character || ' ';
        return character.repeat(count) + this.toString();
    };
    
  4. padLeft(length, char)
    String.prototype.padLeft = function(length, char) {
      return Array(length - this.length + 1).join(char || " ") + this;
    };
    
  5. padLeft(max, c)
    if (!String.prototype.padLeft) {
      String.prototype.padLeft = function(max, c) {
        var self = this;
        return new Array(Math.max(0, max - self.length + 1)).join(c || ' ') + self;
      };
    
  6. padLeft(n, str)
    String.prototype.padLeft = function(n, str) {
      return Array(n - String(this).length + 1).join(str || '0') + this;
    
  7. padLeft(pad, len)
    String.prototype.padLeft = function (pad, len)
      if (typeof(len) == "undefined") { var len = 0; }
      if (typeof(pad) == "undefined") { var pad = ' '; }
      if (len + 1 >= this.length)
          str = Array(len + 1 - this.length).join(pad) + this;
        return str;
    ...
    
  8. padLeft(padString,length)
    String.prototype.padLeft = function(padString,length){
        var str = this;
        while (str.length < length)
            str = padString + str;
        return str;
    
  9. padLeft(paddingValue)
    String.prototype.padLeft = function (paddingValue) {
      return String(paddingValue + this).slice(-paddingValue.length);
    };