Last active
December 7, 2017 01:19
-
-
Save jawdatls/42fd1f43ae9ce729b9ce to your computer and use it in GitHub Desktop.
Javascript strPad function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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