472

I'm trying to insert a certain number of indentations before a string based on an items depth and I'm wondering if there is a way to return a string repeated X times. Example:

string indent = "---";
Console.WriteLine(indent.Repeat(0)); //would print nothing.
Console.WriteLine(indent.Repeat(1)); //would print "---".
Console.WriteLine(indent.Repeat(2)); //would print "------".
Console.WriteLine(indent.Repeat(3)); //would print "---------".
1

21 Answers 21

796

If you only intend to repeat the same character you can use the string constructor that accepts a char and the number of times to repeat it new String(char c, int count).

For example, to repeat a dash five times:

string result = new String('-', 5);
Output: -----
6
  • 13
    Thanks for reminding me about this nice little feature. I think it was lost in the corner of my mind. Commented Sep 20, 2010 at 19:18
  • 127
    This is a very useful trick of C#, but the title of the question is asking about a string (not a char). The other answers below this one are actually answering the question, but are rated much lower. I'm not trying to disrespect Ahmad's answer, but I think either the title of this question should be changed (if the question is actually regarding characters) or the other answers really should be upvoted (and not this one). Commented Mar 13, 2013 at 16:16
  • 17
    @Ahmad As I said, I did not downvote because I don't think you provided good information, but because the information you provided is not relevant to answering the question as currently stated. Imagine if you were searching for a way to repeat strings (not chars) and when you get to the relevant question you have to scroll through several 'useful' tips to get to the actual answers to the question. If the downvote is offense to you, I can remove it. But do you see where I'm coming from? Am I totally off-base here? Commented Mar 13, 2013 at 16:42
  • 14
    @pghprogrammer4 IMHO, downvotes can be quite discouraging, and should only be used for harmful/misleading/truly_bad answers, that need a major change or should be deleted by their author. Answers exist because people are generous and wish to share what they know. Generally, these sites really on the encouragement of upvotes to bring useful answers to the top. Commented Mar 18, 2014 at 4:17
  • 5
    This does not answer the question. I need to repeat a string. Not one single char.
    – Tvde1
    Commented Jun 21, 2019 at 19:25
353

If you're using .NET 4.0, you could use string.Concat together with Enumerable.Repeat.

int N = 5; // or whatever
Console.WriteLine(string.Concat(Enumerable.Repeat(indent, N)));

Otherwise I'd go with something like Adam's answer.

The reason I generally wouldn't advise using Andrey's answer is simply that the ToArray() call introduces superfluous overhead that is avoided with the StringBuilder approach suggested by Adam. That said, at least it works without requiring .NET 4.0; and it's quick and easy (and isn't going to kill you if efficiency isn't too much of a concern).

3
  • 3
    This will work nicely with strings - like if you need to concat non-breaking spaces ( ). But I assume the string constructor will be fastest if you're dealing with a char...
    – woodbase
    Commented Sep 12, 2012 at 11:52
  • 1
    String constructor will only work for one char. to option of using Concat Repeat will work for strings
    – Eli Dagan
    Commented Dec 12, 2016 at 10:00
  • If you are getting -System.Linq.Enumerable etc as output, it's because you (ok, this was me) were too quick to copy/paste and modify the snippet. If you need to concat a different string onto the output of the Enumerable.Repeat, you need to do it like this: Console.WriteLine(string.Concat("-", string.Concat(Enumerable.Repeat(indent, N))));
    – Gabe
    Commented Feb 11, 2020 at 14:22
66

most performant solution for string

string result = new StringBuilder().Insert(0, "---", 5).ToString();
2
  • 6
    Sure is a pretty one-liner. Sure not the most performant. StringBuilder has a little overhead over very little number of concatenations.
    – Teejay
    Commented Mar 26, 2018 at 10:26
  • 2
    even more performant should be: string result = new StringBuilder("---".Length * 5).Insert(0, "---", 5).ToString();
    – Andrew
    Commented Jul 5, 2021 at 16:18
54
public static class StringExtensions
{
    public static string Repeat(this string input, int count)
    {
        if (string.IsNullOrEmpty(input) || count <= 1)
            return input;

        var builder = new StringBuilder(input.Length * count);

        for(var i = 0; i < count; i++) builder.Append(input);

        return builder.ToString();
    }
}
4
  • 7
    Might as well pass input.Length * count to the StringBuilder constructor, don't you think?
    – Dan Tao
    Commented Sep 20, 2010 at 19:16
  • Adam, I see you went to the trouble of testing input for null when adding it to count, but then allowed that to fall through, relying on Append to do nothing given the null. IMHO this is less than obvious: if null is a possible input, why not test for that up front, and return an empty string? Commented Mar 18, 2014 at 4:33
  • 2
    @AdamRobinson Is returning an empty string in place of a null desirable? I'd replace return string.empty with return input, that way you get null in/null out, and empty in/empty out.
    – MattDavey
    Commented Nov 7, 2017 at 12:53
  • 1
    Need to check count > 0 or constructing StringBuilder will throw an exception. Commented Apr 10, 2018 at 16:09
43

For many scenarios, this is probably the neatest solution:

public static class StringExtensions
{
    public static string Repeat(this string s, int n)
        => new StringBuilder(s.Length * n).Insert(0, s, n).ToString();
}

Usage is then:

text = "Hello World! ".Repeat(5);

This builds on other answers (particularly @c0rd's). As well as simplicity, it has the following features, which not all the other techniques discussed share:

  • Repetition of a string of any length, not just a character (as requested by the OP).
  • Efficient use of StringBuilder through storage preallocation.
0
37

Strings and chars [version 1]

string.Join("", Enumerable.Repeat("text" , 2 ));    
//result: texttext

Strings and chars [version 2]:

String.Concat(Enumerable.Repeat("text", 2));
//result: texttext

Strings and chars [version 3]

new StringBuilder().Insert(0, "text", 2).ToString(); 
//result: texttext

Chars only:

new string('5', 3);
//result: 555

Extension way:

(works FASTER - better for WEB)

public static class RepeatExtensions
{
    public static string Repeat(this string str, int times)
    {
        var a = new StringBuilder();
        
        //Append is faster than Insert
        ( () => a.Append(str) ).RepeatAction(times) ;
        
        return a.ToString();
    }

    public static void RepeatAction(this Action action, int count)
    {
        for (int i = 0; i < count; i++)
        {
            action();
        }
    }

}

usage:

 var a = "Hello".Repeat(3); 
 //result: HelloHelloHello
5
  • Yours should be the accepted answer. Here is a nice usage in Html: var indents = string.Join("", Enumerable.Repeat("&nbsp;", Tabs * 3)); Commented Dec 31, 2020 at 7:14
  • 1
    @JohnErnest for web better to use extension that I have added right now. I have write it because of insertion is relatively long operation than appending Commented Dec 31, 2020 at 11:16
  • my use case was writing a simple Roslyn CSharpSyntaxWalker for a SyntaxTree in a browser window, neither tabs or spaces by themselves would indent, unless I had used a CSS rule. Commented Dec 31, 2020 at 21:58
  • 2
    RepeatAction seems like an unnecessary obscure way to do for.
    – NetMage
    Commented Jan 11, 2021 at 23:38
  • yes and no both. Using this you can do RepeatAction in another places without for syntax. :) This is also useful in code. Not only at this place. Commented Jan 12, 2021 at 14:15
29

Use String.PadLeft, if your desired string contains only a single char.

public static string Indent(int count, char pad)
{
    return String.Empty.PadLeft(count, pad);
}

Credit due here

5
  • 1
    I dunno, this seems to solve the problem as phrased with some elegance. And btw, who are you calling 'S'? :-) Commented Sep 20, 2010 at 19:20
  • 13
    But isn't this really just a less obvious version of the string constructor that takes a char and an int?
    – Dan Tao
    Commented Sep 20, 2010 at 19:26
  • It would look nicer with String.Empty instead of ""... otherwise, good solution ;) Commented Sep 20, 2010 at 19:26
  • @Steve BTW the String.PadLeft arguments are reversed. The count comes before the padding character. Commented Sep 20, 2010 at 19:32
  • padLeft is really slow Commented Feb 25, 2023 at 23:40
24

You can repeat your string (in case it's not a single char) and concat the result, like this:

String.Concat(Enumerable.Repeat("---", 5))
13

I would go for Dan Tao's answer, but if you're not using .NET 4.0 you can do something like that:

public static string Repeat(this string str, int count)
{
    return Enumerable.Repeat(str, count)
                     .Aggregate(
                        new StringBuilder(str.Length * count),
                        (sb, s) => sb.Append(s))
                     .ToString();
}
2
  • 4
    If I'll findthis snippet in my project, I'll wonder who's been violating the KISS and why ... nice otherwise
    – Noctis
    Commented Aug 12, 2014 at 5:24
  • Your new StringBuilder should at least pass in str.Length*count.
    – NetMage
    Commented Jan 11, 2021 at 23:37
4
        string indent = "---";
        string n = string.Concat(Enumerable.Repeat(indent, 1).ToArray());
        string n = string.Concat(Enumerable.Repeat(indent, 2).ToArray());
        string n = string.Concat(Enumerable.Repeat(indent, 3).ToArray());
0
3

Adding the Extension Method I am using all over my projects:

public static string Repeat(this string text, int count)
{
    if (!String.IsNullOrEmpty(text))
    {
        return String.Concat(Enumerable.Repeat(text, count));
    }
    return "";
}

Hope someone can take use of it...

0
3

I like the answer given. Along the same lines though is what I've used in the past:

"".PadLeft(3*Indent,'-')

This will fulfill creating an indent but technically the question was to repeat a string. If the string indent is something like >-< then this as well as the accepted answer would not work. In this case, c0rd's solution using StringBuilder looks good, though the overhead of StringBuilder may in fact not make it the most performant. One option is to build an array of strings, fill it with indent strings, then concat that. To whit:

int Indent = 2;
        
string[] sarray = new string[6];  //assuming max of 6 levels of indent, 0 based

for (int iter = 0; iter < 6; iter++)
{
    //using c0rd's stringbuilder concept, insert ABC as the indent characters to demonstrate any string can be used
    sarray[iter] = new StringBuilder().Insert(0, "ABC", iter).ToString();
}

Console.WriteLine(sarray[Indent] +"blah");  //now pretend to output some indented line

We all love a clever solution but sometimes simple is best.

3

Surprised nobody went old-school. I am not making any claims about this code, but just for fun:

public static string Repeat(this string @this, int count)
{
    var dest = new char[@this.Length * count];
    for (int i = 0; i < dest.Length; i += 1)
    {
        dest[i] = @this[i % @this.Length];
    }
    return new string(dest);
}
8
  • Obviously I left that in as a test for the astute reader :) Very astute, thanks. Commented Oct 14, 2013 at 15:37
  • 1
    There's nothing old-school about naming a parameter @this, that's always been a horrible idea :) Commented Nov 5, 2013 at 5:12
  • 2
    I prefer not to use keyword as variable names unless forced (such as @class = "something" when working with MVC because you need class referring to CSS but it's a keyword in C# (of course). This isn't just me talking, it's a general rule of thumb. Although it's certainly compilable code and legitimate, think about ease of reading and typing as well, both practical reasons. I'm very partial to using parameter names to describe what's happening first so Repeat(this string seed, int count) might be more illustrative imho. Commented Nov 7, 2013 at 4:45
  • 1
    Interesting, thanks. I think in this case you're probably right that @this is less descriptive than it could be. However in general I think it is quite common to be extending classes or interfaces where the only reasonable name for 'this' is rather generic. If you have a parameter called 'instance' or 'it' or 'str' (see MSDN) then what you probably meant is 'this'. Commented Nov 7, 2013 at 13:00
  • 1
    I basically agree with you, but for the sake of argument... since I know more about the string I'm building I can do a marginally better job than StringBuilder. StringBuilder has to make a guess when it's allocating memory. Granted it's unlikely to matter in many scenarios, but somebody out there might be building an enormous number of repeated strings in parallel and be glad to gain a few clock cycles back :) Commented Mar 18, 2014 at 11:37
2

For general use, solutions involving the StringBuilder class are best for repeating multi-character strings. It's optimized to handle the combination of large numbers of strings in a way that simple concatenation can't and that would be difficult or impossible to do more efficiently by hand. The StringBuilder solutions shown here use O(N) iterations to complete, a flat rate proportional to the number of times it is repeated.

However, for very large number of repeats, or where high levels of efficiency must be squeezed out of it, a better approach is to do something similar to StringBuilder's basic functionality but to produce additional copies from the destination, rather than from the original string, as below.

    public static string Repeat_CharArray_LogN(this string str, int times)
    {
        int limit = (int)Math.Log(times, 2);
        char[] buffer = new char[str.Length * times];
        int width = str.Length;
        Array.Copy(str.ToCharArray(), buffer, width);

        for (int index = 0; index < limit; index++)
        {
            Array.Copy(buffer, 0, buffer, width, width);
            width *= 2;
        }
        Array.Copy(buffer, 0, buffer, width, str.Length * times - width);

        return new string(buffer);
    }

This doubles the length of the source/destination string with each iteration, which saves the overhead of resetting counters each time it would go through the original string, instead smoothly reading through and copying the now much longer string, something that modern processors can do much more efficiently.

It uses a base-2 logarithm to find how many times it needs to double the length of the string and then proceeds to do so that many times. Since the remainder to be copied is now less than the total length it is copying from, it can then simply copy a subset of what it has already generated.

I have used the Array.Copy() method over the use of StringBuilder, as a copying of the content of the StringBuilder into itself would have the overhead of producing a new string with that content with each iteration. Array.Copy() avoids this, while still operating with an extremely high rate of efficiency.

This solution takes O(1 + log N) iterations to complete, a rate that increases logarithmically with the number of repeats (doubling the number of repeats equals one additional iteration), a substantial savings over the other methods, which increase proportionally.

1

Another approach is to consider string as IEnumerable<char> and have a generic extension method which will multiply the items in a collection by the specified factor.

public static IEnumerable<T> Repeat<T>(this IEnumerable<T> source, int times)
{
    source = source.ToArray();
    return Enumerable.Range(0, times).SelectMany(_ => source);
}

So in your case:

string indent = "---";
var f = string.Concat(indent.Repeat(0)); //.NET 4 required
//or
var g = new string(indent.Repeat(5).ToArray());
1

I don't have enough rep to comment on Adam's answer, but the best way to do it imo is like this:

public static string RepeatString(string content, int numTimes) {
        if(!string.IsNullOrEmpty(content) && numTimes > 0) {
            StringBuilder builder = new StringBuilder(content.Length * numTimes);

            for(int i = 0; i < numTimes; i++) builder.Append(content);

            return builder.ToString();
        }

        return string.Empty;
    }

You must check to see if numTimes is greater then zero, otherwise you will get an exception.

1

Using the new string.Create function, we can pre-allocate the right size and copy a single string in a loop using Span<char>.

I suspect this is likely to be the fastest method, as there is no extra allocation at all: the string is precisely allocated.

 public static string Repeat(this string source, int times)
 {
     return string.Create(source.Length * times, source, RepeatFromString);
 }
 
 private static void RepeatFromString(Span<char> result, string source)
 {
     ReadOnlySpan<char> sourceSpan = source.AsSpan();
     for (var i = 0; i < result.Length; i += sourceSpan.Length)
         sourceSpan.CopyTo(result.Slice(i, sourceSpan.Length));
 }

dotnetfiddle

0

Not sure how this would perform, but it's an easy piece of code. (I have probably made it appear more complicated than it is.)

int indentCount = 3;
string indent = "---";
string stringToBeIndented = "Blah";
// Need dummy char NOT in stringToBeIndented - vertical tab, anyone?
char dummy = '\v';
stringToBeIndented.PadLeft(stringToBeIndented.Length + indentCount, dummy).Replace(dummy.ToString(), indent);

Alternatively, if you know the maximum number of levels you can expect, you could just declare an array and index into it. You would probably want to make this array static or a constant.

string[] indents = new string[4] { "", indent, indent.Replace("-", "--"), indent.Replace("-", "---"), indent.Replace("-", "----") };
output = indents[indentCount] + stringToBeIndented;      
0

Print a line with repetition.

Console.Write(new string('=', 30) + "\n");

==============================

2
  • 7
    Why didn't you use Console.WriteLine instead of appending \n at the end of the string? Commented May 9, 2020 at 12:30
  • This has already been provided as an answer. Please upvote the existing one instead of adding the same answer again. Thanks. If all 700 people who've upvoted that answer decided to post it again the thread would be unusable.
    – ggorlen
    Commented Apr 16, 2023 at 20:46
-4

You can create an ExtensionMethod to do that!

public static class StringExtension
{
  public static string Repeat(this string str, int count)
  {
    string ret = "";

    for (var x = 0; x < count; x++)
    {
      ret += str;
    }

    return ret;
  }
}

Or using @Dan Tao solution:

public static class StringExtension
{
  public static string Repeat(this string str, int count)
  {
    if (count == 0)
      return "";

    return string.Concat(Enumerable.Repeat(indent, N))
  }
}
2
  • 4
    See my comment to Kyle's answer Commented Sep 20, 2010 at 19:25
  • 1
    Will be a good and fast answer if use StringBuilder instead of String. But at the moment its bad. Commented Dec 31, 2020 at 12:24
-4

I didn't see this solution. I find it simpler for where I currently am in software development:

public static void PrintFigure(int shapeSize)
{
    string figure = "\\/";
    for (int loopTwo = 1; loopTwo <= shapeSize - 1; loopTwo++)
    {
        Console.Write($"{figure}");
    }
}
1
  • This trivially prints something that visually happens to resemble the desired result, but doesn't create a new string you can continue to use programmatically, as asked.
    – ggorlen
    Commented Apr 16, 2023 at 20:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.