Eddie Garmon's Weblog

some architecture, some c#

Quickest way to create a repeating string in c#?

int repeatCount = 10;
string value = "hello world.";
string repeated = string.Join(value, new string[repeatCount + 1]);


Alex said:

How about ...

public static string RepeatString(string
stringToRepeat, int repeatCount)
StringBuilder sb = new
StringBuilder(stringToRepeat.Length *

for (int i=0; i < repeatCount; i++)

return sb.ToString();
# January 12, 2005 6:29 PM

Eddie Garmon said:

I thought so too until I profiled it. You would be supprised at the performance gain in string.Join(), even if you create the StringBuilder with enough memory to hold the final string.
# January 12, 2005 7:10 PM

bigjuju said:

that's purty clever, mister. i'm always up for new string manipulation tricks.
# February 5, 2005 5:08 PM

Avi Nahir said:

Wow! You're using the string as a separator to join empty strings. That's cool and twisted!

# November 21, 2007 9:51 AM

myourshaw said:

how about

string str = new string('?',99).Replace("?","foo")

ugly, but it works

# February 13, 2009 11:51 PM