Can I "multiply" a string (in C#)?
In .NET 4 you can do this:
String.Concat(Enumerable.Repeat("Hello", 4))
Unfortunately / fortunately, the string class is sealed so you can't inherit from it and overload the * operator. You can create an extension method though:
public static string Multiply(this string source, int multiplier)
{
StringBuilder sb = new StringBuilder(multiplier * source.Length);
for (int i = 0; i < multiplier; i++)
{
sb.Append(source);
}
return sb.ToString();
}
string s = "</li></ul>".Multiply(10);
Note that if your "string" is only a single character, there is an overload of the string constructor to handle it:
int multipler = 10;
string TenAs = new string ('A', multipler);