Reputation: 13161
Is there a function in C# that returns x times of a given char or string? Or must I code it myself?
Upvotes: 56
Views: 48651
Reputation: 442
The best solution is the built in string function:
Strings.StrDup(2, "a")
Upvotes: 9
Reputation: 2939
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
Upvotes: 34
Reputation: 77556
string.Join("", Enumerable.Repeat("ab", 2));
Returns
"abab"
And
string.Join("", Enumerable.Repeat('a', 2))
Returns
"aa"
Upvotes: 65