I have the following function that takes a string as parameter and repeats it a number of times (also a parameter). I feel like this is something that's already in the framework or at least could be done better. Any suggestions?
private string chr(string s, int repeat)
{
string result = string.Empty;
for (int i = 0; i < repeat; i++)
{
result += s;
}
return result;
}
The most important improvement you could make to your function is to give it a descriptive name.
Not the most efficient, but concise:
.NET 4:
String.Join(String.Empty, Enumerable.Repeat(s, repeat));
.NET 3.0/3.5:
String.Join(String.Empty, Enumerable.Repeat(s, repeat).ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With