Can I "multiply" a string (in C#)?

inspite picture inspite · Feb 10, 2009 · Viewed 72.1k times · Source

Suppose I have a string, for example,

string snip =  "</li></ul>";

I want to basically write it multiple times, depending on some integer value.

string snip =  "</li></ul>";
int multiplier = 2;

// TODO: magic code to do this 
// snip * multiplier = "</li></ul></li></ul>";

EDIT: I know I can easily write my own function to implement this, I was just wondering if there was some weird string operator that I didn't know about

Answer

Will Dean picture Will Dean · Jul 21, 2010

In .NET 4 you can do this:

String.Concat(Enumerable.Repeat("Hello", 4))