Is there a built-in function to repeat a string or char in .NET? Is there a built-in function to repeat a string or char in .NET? asp.net asp.net

Is there a built-in function to repeat a string or char in .NET?


string.Join("", Enumerable.Repeat("ab", 2));

Returns

"abab"

And

string.Join("", Enumerable.Repeat('a', 2))

Returns

"aa"


string.Concat(Enumerable.Repeat("ab", 2));

returns

"abab"


For strings you should indeed use Kirk's solution:

string.Join("", Enumerable.Repeat("ab", 2));

However for chars you might as well use the built-in (more efficient) string constructor:

new string('a', 2); // returns aa