Left and right pad string - Node.js String

Node.js examples for String:Padding

Description

Left and right pad string

Demo Code


/** //  w  w w . j av  a2 s .co m
 * Left pad
 * @param {string} [character="0"]
 * @param {int} [count=2]
 */
String.prototype.lpad = function(character, count) {
  var ch = character || "0";
  var cnt = count || 2;

  var s = "";
  while (s.length < (cnt - this.length)) { s += ch; }
  s = s.substring(0, cnt-this.length);
  return s+this;
}

/** 
 * Right pad
 * @param {string} [character="0"]
 * @param {int} [count=2]
 */
String.prototype.rpad = function(character, count) {
  var ch = character || "0";
  var cnt = count || 2;

  var s = "";
  while (s.length < (cnt - this.length)) { s += ch; }
  s = s.substring(0, cnt-this.length);
  return this+s;
}

Related Tutorials