Friday, 6 December 2013

Repeat a string N times in C#

Sometimes I want to repeat a given string several times for formatting purposes. The following code can be either made into an extension method with slight changes or a utility method:

string.Join("", Enumerable.Repeat("a", 8).ToArray());

The code above repeat "a" 8 times and returns "aaaaaaaa".