Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  29] [ 1]  / answers: 1 / hits: 31777  / 14 Years ago, tue, december 28, 2010, 12:00:00

In JavaScript, how would I create a string of repeating strings x number of times:



var s = new String( ,3);

//s would now be    

More From » javascript

 Answers
15

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

[#94460] Monday, December 27, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
noe

Total Points: 149
Total Questions: 91
Total Answers: 91

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
;