JavaScript Pad function

Created 02 December 2004 00:00

This function allows you to pad strings e.g. to pad a date part with 0's, you'd call

yourString = yourString.pad('0', 1);

// chr      Character to pad the string with
// num      The length of the string after padding
// prepend  (Optional) True to add to beginning, false for end - defaults to true.
String.prototype.pad = function(chr, num, prepend) {
  var front = true;
  var str = this;
  if (arguments.length > 2) front = prepend;
  for (var i=str.length; i<=num; i++) {
    str = ((front) ? (chr + str) : (str + chr));
  }
  return str;
}