The fastest easiest way to get it right.

pad

NAME

String.pad

FUNCTION

The Javascript function pad pads out a string to a given length on either left or right with any character.

INPUTS

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

Example usage:

     var x = 'abc';
     var y = x.pad(6,'0','L');  // returns '000abc';

SOURCE

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;
}

User Comments

There are no user comments yet on this page.


Add A Comment

Comments will not appear until after they are moderated. Comments are usually moderated within a few hours on weekdays, but may take longer on weekends and holidays.

Name or nickname: (This will appear with your comment)


Email (this will never be displayed)


Enter your comment here. Use [b] and [/b] for bold, [i] and [/i] for italic, and [pre] and [/pre] for code samples. All literal HTML and PHP that you enter will be escaped out and displayed as you enter it.


Home |  Documentation |  Download |  Credits |  Contact |  Login
Andromeda © Copyright 2004-2010, Licensed under the GPL Version 2