Let’s say I’ve a string str = "squirrel" and I want to have a string str = "squirrelsquirrelsquirrel".
I wrote a function:
public static string RepeatString(string str, int n)
{
string s = "";
for (int i = 0; i < n; i++)
s += str;
return s;
}
But it seems to me that this code looks very unoptimized. Are there faster methods to achieve the desired result?
>Solution :
From the answer here:
In .NET 4 you can do this:
String.Concat(Enumerable.Repeat("Hello", 4))
In your case,
String.Concat(Enumerable.Repeat("squirrel", 3))