Regex.Replace, String.Replace or StringBuilder.Replace which is the fastest?

前端 未结 2 550
生来不讨喜
生来不讨喜 2021-01-25 03:21

I need to replace all System.Environment.Newline(s) in the string returned by my function with System.Environment.Newline + \\t (as I am trying to appl

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-25 04:10

    If you're just trying to do it within a single string, I'd expect string.Replace to be as fast as anything else. StringBuilder is useful when you want to perform a number of separate steps and want to avoid creating an intermediate string on each step.

    Have you benchmarked string.Replace to find out whether or not it's fast enough for you?

    I would personally only start using regular expressions when I was actually dealing with a pattern, rather than just a fixed sequence of characters. If the performance of this is absolutely crucial, you could benchmark that as well of course.

提交回复
热议问题