A comprehensive JavaScript pad function
I wanted a JavaScript function for padding strings, but I couldn't find one that met my criteria. So, I wrote my own.
- Minified (850 bytes)
- Uncompressed
It behaves identically to PHP's str_pad() function, and has been unit tested in Firefox 3, Safari 3, Chrome, IE6, and IE7.
It's optimized for speed, not readability. (It processes 500 sets of randomly generated parameters in less than 50ms in all of the above-listed browsers on my 3.0GHz Pentium 4, and less than 100ms in IE6 on my aging Toshiba notebook.)
Syntax:
foo.pad( padLength [, padString = " " [, padType = "right" ] ] );
...where foo is any String object.
padType must be one of "left", "right", or "both". Otherwise, the parameters are identical to those for str_pad().
Comments
No comments yet.