Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a LINQ-way to append the same character n-times to a string?

Tags:

c#

linq

I want to create a string repeating the same seqeuence n-times.

How I do this:

var sequence = "\t";
var indent = string.Empty;   

for (var i = 0; i < n; i++)
{
    indent += sequence;
}

Is there a neat LINQ equivalent to accomplish the same result?

like image 261
boop Avatar asked Aug 07 '19 16:08

boop


1 Answers

You can use Enumerable.Repeat in String.Concat:

string intend = String.Concat(Enumerable.Repeat(sequence, n));

If you just want to repeat a single character you should prefer the String-constructor:

string intend = new String('\t', n);
like image 153
Tim Schmelter Avatar answered Oct 24 '22 22:10

Tim Schmelter