Is there a built in function to pad characters n
number of times?
Ex:
var myString = 'something';
var myCharToPad = '-';
var numTimesToPad = 5;
I am expecting myString = 'something-----';
Is there a built in function to pad characters n
number of times?
Ex:
var myString = 'something';
var myCharToPad = '-';
var numTimesToPad = 5;
I am expecting myString = 'something-----';
No, but it's not hard:
myString += new Array(numTimesToPad+1).join(myCharToPad);
Nope, but it's trivial to write one:
function pad(num, string, char) {
for (var i = 0; i < num; i++) {
string += char;
}
return string;
}
var myString = pad(5, 'something', '-')
This is a repeated entry; you should always check first and if nobody has an answer, which is almost impossible [sorry but chances are that you are not that special] then post... that even saves you time by solving your issue faster. You can find my "almost" ultimate answer to the JS padding issue at here! also on this site.