Best way to repeat a character in C#


In all versions of .NET, you can repeat a string thus:

public static string Repeat(string value, int count)
    return new StringBuilder(value.Length * count).Insert(0, value, count).ToString();

To repeat a character, new String('\t', count) is your best bet. See the answer by @CMS.


What it's the best way to generate a string of \t's in C#

I am learning C# and experimenting with different ways of saying the same thing.

Tabs(uint t) is a function that returns a string with t amount of \t's

For example Tabs(3) returns "\t\t\t"

Which of these three ways of implementing Tabs(uint numTabs) is best?

Of course that depends on what "best" means.

  1. The LINQ version is only two lines, which is nice. But are the calls to Repeat and Aggregate unnecessarily time/resource consuming?

  2. The StringBuilder version is very clear but is the StringBuilder class somehow slower?

  3. The string version is basic, which means it is easy to understand.

  4. Does it not matter at all? Are they all equal?

These are all questions to help me get a better feel for C#.

private string Tabs(uint numTabs)
    IEnumerable<string> tabs = Enumerable.Repeat("\t", (int) numTabs);
    return (numTabs > 0) ? tabs.Aggregate((sum, next) => sum + next) : ""; 

private string Tabs(uint numTabs)
    StringBuilder sb = new StringBuilder();
    for (uint i = 0; i < numTabs; i++)

    return sb.ToString();

private string Tabs(uint numTabs)
    string output = "";
    for (uint i = 0; i < numTabs; i++)
        output += '\t';
    return output; 

The best version is certainly to use the builtin way:

string Tabs(int len) { return new string('\t', len); }

Of the other solutions, prefer the easiest; only if this is proving too slow, strive for a more efficient solution.

If you use a StringBuilder and know its resulting length in advance, then also use an appropriate constructor, this is much more efficient because it means that only one time-consuming allocation takes place, and no unnecessary copying of data. Nonsense: of course the above code is more efficient.

And yet another method

new System.Text.StringBuilder().Append('\t', 100).ToString()

What about using extension method?

public static class StringExtensions
   public static string Repeat(this char chatToRepeat, int repeat) {

       return new string(chatToRepeat,repeat);
   public  static string Repeat(this string stringToRepeat,int repeat)
       var builder = new StringBuilder(repeat*stringToRepeat.Length);
       for (int i = 0; i < repeat; i++) {
       return builder.ToString();

You could then write :

Debug.WriteLine('-'.Repeat(100)); // For Chars  
Debug.WriteLine("Hello".Repeat(100)); // For Strings

Note that a performance test of using the stringbuilder version for simple characters instead of strings gives you a major preformance penality : on my computer the difference in mesured performance is 1:20 between: Debug.WriteLine('-'.Repeat(1000000)) //char version and
Debug.WriteLine("-".Repeat(1000000)) //string version

You can create an extension method

static class MyExtensions
    internal static string Repeat(this char c, int n)
        return new string(c, n);

Then you can use it like this


var str = new string(Enumerable.Repeat('\t', numTabs).ToArray());

Using String.Concat and Enumerable.Repeat which will be less expensive than using String.Join

public static Repeat(this String pattern, int count)
    return String.Concat(Enumerable.Repeat(pattern, count));

I know that this question is five years old already but there is a simple way to repeat a string that even works in .Net 2.0.

To repeat a string:

string repeated = new String('+', 3).Replace("+", "Hello, ");


"Hello, Hello, Hello, "

To repeat a string as an array:

// Two line version.
string repeated = new String('+', 3).Replace("+", "Hello,");
string[] repeatedArray = repeated.Split(',');

// One line version.
string[] repeatedArray = new String('+', 3).Replace("+", "Hello,").Split(',');


{"Hello", "Hello", "Hello", ""}

Keep it simple.



c# c#   .net .net   string