Skip to content

Instantly share code, notes, and snippets.

@jawdatls
Last active December 7, 2017 01:19
Show Gist options
  • Save jawdatls/42fd1f43ae9ce729b9ce to your computer and use it in GitHub Desktop.
Save jawdatls/42fd1f43ae9ce729b9ce to your computer and use it in GitHub Desktop.
Javascript strPad function
function str_pad(input, pad_length, pad_string, pad_type){
var output = input.toString();
if (pad_string === undefined) { pad_string = ' '; }
if (pad_type === undefined) { pad_type = 'STR_PAD_RIGHT'; }
if (pad_type == 'STR_PAD_RIGHT') {
while (output.length < pad_length) {
output = output + pad_string;
}
} else if (pad_type == 'STR_PAD_LEFT') {
while (output.length < pad_length) {
output = pad_string + output;
}
} else if (pad_type == 'STR_PAD_BOTH') {
var j = 0;
while (output.length < pad_length) {
if (j % 2) {
output = output + pad_string;
} else {
output = pad_string + output;
}
j++;
}
}
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment