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?
Asked
Active
Viewed 566 times
0

Robin Rodricks
- 110,798
- 141
- 398
- 607
-
2Without any code of your current approach we cannot suggest a better one. But even then it sounds as if you want to post it better at [`codereview.stackexchange.com`](http://codereview.stackexchange.com). – Tim Schmelter Sep 16 '12 at 19:27
-
1it would help if you would show us what you have so far! – Thousand Sep 16 '12 at 19:27
-
http://www.dotnetperls.com/regex-groups may it help you – Gyan Chandra Srivastava Sep 16 '12 at 19:35
-
If you are doing multiple replaces in one string, perhaps this would be useful: http://stackoverflow.com/questions/711753/a-better-way-to-replace-many-strings-obfuscation-in-c-sharp/712058#712058 – Guffa Sep 16 '12 at 19:50
-
U can't modify a String in C# because Strings are immutable. – qwerty Sep 16 '12 at 20:04
1 Answers
2
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?
StringBuilder
class 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.

Omar
- 16,329
- 10
- 48
- 66