String trimming, cleaning, padding - Node.js String

Node.js examples for String:Padding

Description

String trimming, cleaning, padding

Demo Code


String.prototype.Trim = function() { return this.replace(/^\s*|\s*$/g, ""); }

String.prototype.Clean = function() { return this.replace(/[^\w|\s|@|&|.|,|!|%|(|)|+|-]/g, "").replace(/_/g, " ").replace(/\s+/g, " ").Trim(); }


String.prototype.Pad = function(length, padChar) {
  var str = String(this);
  length = length.toInt();
  if (typeof padChar == 'undefined') padChar = " ";
  else {/*from   w w  w  .  ja v a 2s.c  o  m*/
    padChar = String(padChar);
    if (padChar.length < 1) padChar = " ";
  }
  while (str.length < length) str = padChar + str;
  return str;
}

Related Tutorials