Left pad a String - Node.js String

Node.js examples for String:Padding

Description

Left pad a String

Demo Code

/**/*  w  w  w.  j a  va2 s .  c  o m*/
 * Left pad
 *   ie.   "k".lpad("o",5)  --> "ooook"
 * @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;
}

Related Tutorials