Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder.Append for string array?

Say I have a StringBuilder object

var sb = new StringBuilder();

And an arbritrary array of strings

var s = new []{"a","b","c"};

Is this the 'quickest' way to insert them into the stringbuilder instance?

sb.Append(string.join(string.empty, s));

Or does StringBuilder have a function I have overlooked?

Edit: Sorry I dont know how many items sb will contain, or how many items may be in each String[].

like image 389
maxp Avatar asked Dec 14 '25 16:12

maxp


2 Answers

If you mean by "quickest" most performant than better use:

for(int i = 0; i < myArrayLen; i++)
  sb.Append(myArray[i]);
like image 72
Random Dev Avatar answered Dec 16 '25 04:12

Random Dev


string.Concat(...) should be faster than string.Join("", ...). Also, this depends on what else you're doing with your StringBuilder. If you're only performing a few concatenations then it can be faster not to use it.

More context always helps!

like image 33
porges Avatar answered Dec 16 '25 04:12

porges