When to use Regex vs Built in String Methods?

后端 未结 6 700
庸人自扰
庸人自扰 2021-01-12 07:03

I\'ve noticed a lot of little debates about when to use regex and when to use a built in string function like String.Replace() (.NET).

It seems a lot of people recom

6条回答
  •  时光取名叫无心
    2021-01-12 07:42

    Obviously, for complex search/match/replace operations, regexes are the way to go. For simple stuff like replacing a single word by another word, normal string methods are preferred.

    But in many cases, it's not that simple. Sometimes you come across a situation where you could use standard string operations, while the regex solution is more elegant. Even if the vanilla string algorithm is 10 times faster, it's always a good idea to ask yourself if it matters in that particular piece of code (for example if the code isn't executed in a loop).

    I would prefer the readability of a simple regex operation over a more complex, but faster algorithm using pure string operations.

    Just my 2 cents...

提交回复
热议问题