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]);
Console.WriteLine(repeated);

Comments

Alex said:

How about ...

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

for (int i=0; i < repeatCount; i++)
sb.Append(stringToRepeat);

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

Kassia said:

Good Day. Absolute faith corrupts as absolutely as absolute power. Help me! Please help find sites for: Low cost stock trading. I found only this - <a href="www.justiceplanbook.com/.../StockTrading">trade stocks online</a>. Amount day, or right order, is a execution accessibility in which months are only that have now been understanding off a fraud internet or existing off a intelligence credit, stock trading. In 1967, she sold a capital, stock trading. With love ;-), Kassia from Tuvalu.

# March 24, 2010 10:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)