In JavaScript, how would I create a string of repeating strings x number of times:
var s = new String(" ",3);
//s would now be " "
There is no such function, but hey, you can create it:
String.prototype.repeat = function(times) {
return (new Array(times + 1)).join(this);
};
Usage:
var s = " ".repeat(3);
Of course you could write this as part of a standalone group of functions:
var StringUtilities = {
repeat: function(str, times) {
return (new Array(times + 1)).join(str);
}
//other related string functions...
};
Usage:
var s = StringUtilities.repeat(" ", 3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With