string.Repeat() extension method

I needed str_repeat() functionality in one of my C# methods. As .Net currently doesn’t offer it I wrote my own Repeat() extension method for strings. Make sure you put the method inside static class.


public static string Repeat(this string instr, int n)
{
   
var result = string
.Empty;

   
for (var
i = 0; i < n; i++)
        result += instr;
   
return result;
}

If you have some better trick for string repeating then let me know. 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *