How to Return a String Repeated X Number of Times

Is there an easy way to return a string repeated X number of times?

If you only intend to repeat the same character you can use the string constructor that accepts a char and the number of times to repeat it new String(char c, int count).

For example, to repeat a dash five times:

string result = new String('-', 5);
Output: -----

Repeat a string in JavaScript a number of times

These days, the repeat string method is implemented almost everywhere. (It is not in Internet Explorer.) So unless you need to support older browsers, you can simply write:

"a".repeat(10)

Before repeat, we used this hack:

Array(11).join("a") // create string with 10 a's: "How to Return a String Repeated X Number of Timesaa"

(Note that an array of length 11 gets you only 10 "a"s, since Array.join puts the argument between the array elements.)

Simon also points out that according to this benchmark, it appears that it's faster in Safari and Chrome (but not Firefox) to repeat a character multiple times by simply appending using a for loop (although a bit less concise).

Return String of Characters Repeated x Times


char *repeat(const char *s, int x){
if(s){
int i, count = 0;

while(s[count] != '\0'){
++count;
}

char *newArray = malloc(count * x + 1);
if(newArray){
char *na = newArray;
for(i = 0; i < x; ++i) {
const char *p=s;
while(*p)
*na++ = *p++;
}
*na = '\0';
}
return newArray;
} else {
return NULL;
}
}

Writing a function that repeats a string n times and separates each repetition with another string

You can use iterable unpacking and only use the print function:

def repeat(word, n, delim):
print(*n*[word], sep=delim)

Or just use str.join:

def repeat(word, n, delim):
print(delim.join(word for _ in range(n)))

How can I recurse in JS to repeat a string n times?

Recursion involves making a function call itself and AVOIDS using a loop. You have a loop in the recursive function which is a big red flag.

Do something like this:
If n is 1 return s else return s concatenated to the result of the function with n - 1.

function repeatStr (n, s) {
if (n == 1)
return s;
else
return s.concat(repeatStr(n - 1, s))
}
repeatStr(3, "Hi")

how can i repeat a string multiple times according to its index in a string

In functional style (thanks to @Redu for the comment):





const accum = (s) => Array.from(

s,

(c, i) => `${c.toLocaleUpperCase()}${c.repeat(i)}`

)

.join('-');


console.log(accum(''));

console.log(accum('a'));

console.log(accum('xyz'));

How to get my string repeated x times?

Here are two proposals; one tries to match the code you posted, and the other tries to match the English you posted. This seems to be mostly a question about syntax, so I'm not sure there's a lot of meaningful explanation that can go along with this other than "read a tutorial".

-- match the code
test :: Int -> String
test k = concat [show (r * r) | r <- [0..k]]

-- match the English
test :: Int -> String -> String
test k s = concat [s | r <- [0..k]]


Related Topics



Leave a reply



Submit