String.Replace() vs. StringBuilder.Replace()

前端 未结 9 1587
萌比男神i
萌比男神i 2020-11-28 05:04

I have a string in which I need to replace markers with values from a dictionary. It has to be as efficient as possible. Doing a loop with a string.replace is just going to

9条回答
  •  清歌不尽
    2020-11-28 05:50

    Would stringbuilder.replace be any better [than String.Replace]

    Yes, a lot better. And if you can estimate an upper bound for the new string (it looks like you can) then it will probably be fast enough.

    When you create it like:

      var sb = new StringBuilder(inputString, pessimisticEstimate);
    

    then the StringBuilder will not have to re-allocate its buffer.

提交回复
热议问题