0

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?

5
  • 2
    Without 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. Commented Sep 16, 2012 at 19:27
  • 1
    it would help if you would show us what you have so far! Commented Sep 16, 2012 at 19:27
  • dotnetperls.com/regex-groups may it help you Commented Sep 16, 2012 at 19:35
  • If you are doing multiple replaces in one string, perhaps this would be useful: stackoverflow.com/questions/711753/… Commented Sep 16, 2012 at 19:50
  • U can't modify a String in C# because Strings are immutable. Commented Sep 16, 2012 at 20:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

The + operator compiles to a call to String.Concat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.