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.

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.

Leave a Reply

(will not be published)
(optional)

What My Clients Say

Travis has worked on several projects for us and we have an ongoing relationship which we value. He has excellent technical skills and communication skills. He is highly effective in taking a project from concept to completion. In addition, he is able to communicate with business professionals effectively.

—Clarence Johnson
FSI Holding Corp.