Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to copy a StringBuilder?

What is the best way (performance wise) to instantiate a new StringBuilder from another one? (Copy-Ctor).

I don't want to pass via an immutable string as the underline data is very big.

so the below is valid, but not wanted.

StringBuilder newSB = new StringBuilder(oldSB.ToString());

I want something like

StringBuilder newSB = new StringBuilder(oldSB);

But this is not supported.

like image 409
NirMH Avatar asked Oct 21 '25 04:10

NirMH


1 Answers

There’s an overload of Append for other StringBuilder instances:

var newSB = new StringBuilder(oldSB.Length);
newSB.Append(oldSB);
like image 68
Ry- Avatar answered Oct 22 '25 18:10

Ry-



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!