Robin Rodricks
Robin Rodricks

Reputation: 113956

Fastest way to find/replace in a large string

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?

Upvotes: 0

Views: 624

Answers (1)

Omar
Omar

Reputation: 16623

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 use String.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.

Upvotes: 2

Related Questions