String.pad
The Javascript function pad pads out a string to a given
length on either left or right with any character.
int - the size of the resulting string
string - the string that should be added. You can provide
a string of more than one character. The resulting string
will be clipped to ensure it is returned at the requested
size.
character - either an 'L' or left padding or an 'R' for
right padding.
Example usage:
var x = 'abc';
var y = x.pad(6,'0','L'); // returns '000abc';
String.prototype.pad = function(size,character,where) {
var val = this;
while(val.length < size) {
if(where == 'L')
val = character + val;
else
val += character;
if(val.length > size) {
val = val.slice(0,size);
}
}
return val;
}