I have a function that does a lot of finding and replacing on strings, using Regex and other string processing functions. Essentially I'm looping through a string, and adding the resulting data into a StringBuilder so its faster than modifying the string itself. Is there a faster way?
1 Answer
Essentially I'm looping through a string, and adding the resulting data into a StringBuilder so its faster than modifying the string itself. Is there a faster way?
StringBuilderclass is faster when you want to concatenate some strings into a loop.If you're concatening an array
String.Concat()is faster bacause it has some overloads which accept arrays.else use simply the
+operator if you have to do something like:string s = "text1" + "text2" + "text3";or useString.Concat("text1", "text2", "text3");.
For more info look here: Concatenate String Efficiently.
EDIT :
The + operator compiles to a call to String.Concat() as said usr in his comment.
1 Comment
+ operator compiles to a call to String.Concat.
codereview.stackexchange.com.