879

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++)
        sb.Append("\t");

    return sb.ToString();
}  

private string Tabs(uint numTabs)
{
    string output = "";
    for (uint i = 0; i < numTabs; i++)
    {
        output += '\t';
    }
    return output; 
}
i3arnon
  • 101,022
  • 27
  • 291
  • 322
Alex Baranosky
  • 44,548
  • 39
  • 95
  • 146

20 Answers20

1617

What about this:

string tabs = new String('\t', n);

Where n is the number of times you want to repeat the string.

Or better:

static string Tabs(int n)
{
    return new String('\t', n);
}
silkfire
  • 20,433
  • 12
  • 70
  • 93
Christian C. Salvadó
  • 723,813
  • 173
  • 899
  • 828
174
string.Concat(Enumerable.Repeat("ab", 2));

Returns

"abab"

And

string.Concat(Enumerable.Repeat("a", 2));

Returns

"aa"

from...

Is there a built-in function to repeat string or char in .net?

Community
  • 1
  • 1
Carter Medlin
  • 10,482
  • 4
  • 55
  • 65
  • 2
    Make it better by doing it without Linq and with `StruingBuilder`! – Bitterblue Apr 15 '14 at 15:29
  • 4
    ''StringBuilder'' is not necessarily faster http://stackoverflow.com/questions/585860/string-join-vs-stringbuilder-which-is-faster – Schiavini Dec 09 '14 at 16:13
  • 6
    It may not be faster, but it can save on memory allocations (pressure) if you specify the correct capacity up front, and that can be as important as the micro benchmark of profiling this. – wekempf Apr 05 '16 at 13:03
  • 4
    var strRepeat = string.Concat(Enumerable.Repeat("ABC", 1000000)); //50ms – yongfa365 Dec 07 '18 at 02:31
132

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.

Community
  • 1
  • 1
Binoj Antony
  • 15,352
  • 24
  • 86
  • 95
  • 7
    This is by far the fastest performance wise for String. Other methods create too much overhead. – midspace May 11 '15 at 00:22
  • @midspace Do you know if anybody has explained why the other answers are worse? I would have assumed the native String.Concat would be just as optimized internally as StringBuilder – Marie Mar 25 '19 at 14:24
  • @Marie The following article explains some of the merits. https://support.microsoft.com/en-au/help/306822/how-to-improve-string-concatenation-performance-in-visual-c Otherwise two other factors are, StringBuilder allows strings and isn't limited to just a char (though that doesn't pertain to the original question), and the string.Concat answer above has to create an array first using Enumerable.Repeat. As I said, it's unnecessary overhead. – midspace Mar 26 '19 at 22:35
  • I had assumed it wouldnt create an array first because it could just iterate item by item. Thank you for the link, I will ggive it a read! – Marie Mar 27 '19 at 14:14
64

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.

Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141
  • 8
    My benchmarks are showing `new string('\t', len)` to be between 2x and 7x faster than the `StringBuilder` approach. Why do you think `StringBuilder` is more efficient in this case? – StriplingWarrior Aug 23 '11 at 16:32
  • 6
    @StriplingWarrior Thanks for correcting this (after it has been standing here for two years, no less!). – Konrad Rudolph Aug 23 '11 at 17:30
52

Extension methods:

public static string Repeat(this string s, int n)
{
    return new String(Enumerable.Range(0, n).SelectMany(x => s).ToArray());
}

public static string Repeat(this char c, int n)
{
    return new String(c, n);
}
bluish
  • 23,093
  • 23
  • 110
  • 171
Rodrick Chapman
  • 5,247
  • 1
  • 27
  • 32
  • 10
    `Enumerable.Range(0, n).SelectMany(x => s)` can be replaced by a simple `Enumerable.Repeat(s, n)`. – Palec Apr 09 '16 at 15:10
  • 5
    @Palec No it cannot. The `SelectMany` will for each single dummy `x` give a sequence of `char` values (since the `string s` implements `IEnumerable`), and all those `char` sequences are concatenated to one long `char` sequence. What you suggest will instead give an `IEnumerable`. It is not the same. – Jeppe Stig Nielsen Dec 20 '17 at 22:37
  • 2
    I was wrong, @JeppeStigNielsen. Thanks for the heads-up. If I really wanted to use `Enumerable.Repeat`, I would have to concatenate the strings together: `string.Concat(Enumerable.Repeat(s, n))`. This has already been [posted before](https://stackoverflow.com/a/15390726/2157640). – Palec Dec 24 '17 at 12:09
42

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++) {
           builder.Append(stringToRepeat);
       }
       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

larsmoa
  • 11,450
  • 5
  • 54
  • 82
Ronnie
  • 4,809
  • 10
  • 44
  • 69
  • 2
    The performance of the string version may be improved by using [`StringBuilder.Insert(Int32, String, Int32)`](https://msdn.microsoft.com/en-us/library/62eb5xsf%28v=vs.110%29.aspx), as [Binoj Anthony answered](http://stackoverflow.com/a/720915/2157640). – Palec Apr 09 '16 at 15:00
24

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, ");

Returns

"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(',');

Returns

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

Keep it simple.

ChaimG
  • 4,628
  • 3
  • 24
  • 39
24

How about this:

//Repeats a character specified number of times
public static string Repeat(char character,int numberOfIterations)
{
    return "".PadLeft(numberOfIterations, character);
}

//Call the Repeat method
Console.WriteLine(Repeat('\t',40));
bluish
  • 23,093
  • 23
  • 110
  • 171
Denys Wessels
  • 241
  • 2
  • 2
  • 5
    haha.. that's what I could think of when I fell into this situation once. But personally I found `new string('\t', 10)` to be the best solution – shashwat Nov 18 '13 at 11:27
22

Let's say you want to repeat '\t' n number of times, you can use;

String.Empty.PadRight(n,'\t')
Amin
  • 294
  • 2
  • 4
  • 1
    Thanks. This is also very fast. http://codereview.stackexchange.com/questions/36391/adding-n-characters-to-a-string – Sunsetquest Apr 08 '16 at 22:03
  • I have always done it this way. Even if wrapped in an extension method it is simpler than other implementations posted here. Note that OP did just want to repeat \t, not strings. – Michael Ribbons Apr 29 '16 at 02:18
17

Your first example which uses Enumerable.Repeat:

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

can be rewritten more compactly with String.Concat:

private string Tabs(uint numTabs)
{       
    return String.Concat(Enumerable.Repeat("\t", (int) numTabs));
}
Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
Ray
  • 169,974
  • 95
  • 213
  • 200
14

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));
}
bradgonesurfing
  • 28,325
  • 12
  • 101
  • 188
9
var str = new string(Enumerable.Repeat('\t', numTabs).ToArray());
w.b
  • 10,250
  • 5
  • 25
  • 44
4

And yet another method

new System.Text.StringBuilder().Append('\t', 100).ToString()
Artyom
  • 3,149
  • 1
  • 28
  • 56
  • Passing the resulting string length to `StringBuilder` constructor saves reallocation. Still, this is more chatty and less efficient than using the `string` constructor that can repeat a given character, as already shown in the accepted answer. – Palec Jan 22 '18 at 13:48
4

For me is fine:

public static class Utils
{
    public static string LeftZerosFormatter(int zeros, int val)
    {
        string valstr = val.ToString();

        valstr = new string('0', zeros) + valstr;

        return valstr.Substring(valstr.Length - zeros, zeros);
    }
}
4

The answer really depends on the complexity you want. For example, I want to outline all my indents with a vertical bar, so my indent string is determined as follows:

return new string(Enumerable.Range(0, indentSize*indent).Select(
  n => n%4 == 0 ? '|' : ' ').ToArray());
Dmitri Nesteruk
  • 20,962
  • 21
  • 90
  • 152
3

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

Console.WriteLine('\t'.Repeat(10));
ivan
  • 56
  • 3
1

Without a doubt the accepted answer is the best and fastest way to repeat a single character.

Binoj Anthony's answer is a simple and quite efficient way to repeat a string.

However, if you don't mind a little more code, you can use my array fill technique to efficiently create these strings even faster. In my comparison tests, the code below executed in about 35% of the time of the StringBuilder.Insert code.

public static string Repeat(this string value, int count)
{
    var values = new char[count * value.Length];
    values.Fill(value.ToCharArray());
    return new string(values);
}

public static void Fill<T>(this T[] destinationArray, params T[] value)
{
    if (destinationArray == null)
    {
        throw new ArgumentNullException("destinationArray");
    }

    if (value.Length > destinationArray.Length)
    {
        throw new ArgumentException("Length of value array must not be more than length of destination");
    }

    // set the initial array value
    Array.Copy(value, destinationArray, value.Length);

    int copyLength, nextCopyLength;

    for (copyLength = value.Length; (nextCopyLength = copyLength << 1) < destinationArray.Length; copyLength = nextCopyLength)
    {
        Array.Copy(destinationArray, 0, destinationArray, copyLength, copyLength);
    }

    Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength);
}

For more about this array fill technique, see Fastest way to fill an array with a single value

Grax32
  • 3,598
  • 1
  • 15
  • 29
0

Try this:

  1. Add Microsoft.VisualBasic reference
  2. Use: String result = Microsoft.VisualBasic.Strings.StrDup(5,"hi");
  3. Let me know if it works for you.
LarsTech
  • 77,282
  • 14
  • 135
  • 204
  • Adding dependence on another assembly and the need to load it is unreasonable in most cases when you need such functionality. Still, good to know there is such a thing in .NET. – Palec Dec 24 '17 at 15:58
  • There many pros and cons in different contexts, so everyone should decide depending on the situation she/he is in. – Toso Pankovski Dec 25 '17 at 19:01
0

Fill the screen with 6,435 z's $str = [System.Linq.Enumerable]::Repeat([string]::new("z", 143), 45)

$str

David Morrow
  • 85
  • 1
  • 5
-1

Albeit very similar to a previous suggestion, I like to keep it simple and apply the following:

string MyFancyString = "*";
int strLength = 50;
System.Console.WriteLine(MyFancyString.PadRight(strLength, "*");

Standard .Net really,

Porky
  • 630
  • 5
  • 6
  • It will just add padding, does not repeat – levi Feb 06 '19 at 21:08
  • @levi: What is the problem with padding? Does `string result = string.Empty.PadRight('\t',5)` not return the desired result (in this case, 5 tabs)? – J.Lehmann Aug 12 '20 at 07:43