Global.String.prototype.pad (str, len, mode)
fills a string with another string up to a desired length

  • Parameter String:
    the filling string
  • Parameter Number:
    the desired length of the resulting string
  • Parameter Number:
    the direction which the string will be padded in: -1: left 0: both (balance) 1: right (you can use the constants String.LEFT, String.BALANCE and String.RIGHT here as well.)
  • Returns
    String the resulting string
Sourcecode in /home/hannes/helma/modules/core/String.js:
495:   String.prototype.pad = function(str, len, mode) {
496:       if (str  == null || len == null)
497:           return this;
498:       var diff = len - this.length;
499:       if (diff == 0)
500:           return this;
501:       var left, right = 0;
502:       if (mode == null || mode == String.RIGHT)
503:           right = diff;
504:      else if (mode == String.LEFT)
505:          left = diff;
506:      else if (mode == String.BALANCE) {
507:          right = Math.round(diff / 2);
508:          left = diff - right;
509:      }
510:      res.push();
511:      for (var i=0; i<left; i++)
512:          res.write(str);
513:      res.write(this);
514:      for (var i=0; i<right; i++)
515:          res.write(str);
516:      return res.pop();
517:  }