boop
boop

Reputation: 7787

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

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?

Upvotes: 8

Views: 1037

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460350

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

Upvotes: 9

Related Questions