Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder append same char multiple times

Is there a fast way to append a StringBuilder multiple times in C#? Of course I can write my own extension to append in loop, but it looks ineffective.

like image 979
Piotr Stapp Avatar asked Aug 24 '26 04:08

Piotr Stapp


2 Answers

There is already an Append overload that accepts char value and int repeatCount:

char c = ...
int count = ...
builder.Append(c, count); 

or more simply (in many cases):

builder.Append(' ', 20);
like image 192
Marc Gravell Avatar answered Aug 25 '26 19:08

Marc Gravell


You can use string constructor

sb.Append(new String('a', 3)); // equals  sb.Append("aaa")
like image 20
Mehmet Ataş Avatar answered Aug 25 '26 19:08

Mehmet Ataş



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!