Search code examples
c#regexstringperformance

What's faster: Regex or string operations?


When should I use Regex over string operations and vice versa only regarding performance?


Solution

  • It depends

    Although string manipulation will usually be somewhat faster, the actual performance heavily depends on a number of factors, including:

    • How many times you parse the regex
    • How cleverly you write your string code
    • Whether the regex is precompiled

    As the regex gets more complicated, it will take much more effort and complexity to write equivalent string manipulation code that performs well.