Is there a built-in function to repeat a string or char in .NET?
C#asp.netvb.netC# Problem Overview
Is there a function in C# that returns x times of a given char or string? Or must I code it myself?
C# Solutions
Solution 1 - C#
string.Join("", Enumerable.Repeat("ab", 2));
Returns
"abab"
And
string.Join("", Enumerable.Repeat('a', 2))
Returns
"aa"
Solution 2 - C#
string.Concat(Enumerable.Repeat("ab", 2));
returns
> "abab"
Solution 3 - C#
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
Solution 4 - C#
new String('*', 5)
See Rosetta Code.
Solution 5 - C#
The best solution is the built in string function:
Strings.StrDup(2, "a")