Trims a string to len - Node.js String

Node.js examples for String:Strip

Description

Trims a string to len

Demo Code


/**/*from   w w  w  .  ja va  2s .co m*/
 * Trims a string to len 
 * @method String.trim
 * @param {Integer} len is the length of the required string
 * @param {String} p is the string to prepend to the returned string
 */
String.prototype.trim = function(len,p) {
    if (this.length <= len) {
    return this.toString();
  }

    var output=[];

    for (var i = 0; i < len; i++) {
    output.push(this[i]);
  }

    return output.join("")+p;
};

Related Tutorials