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

前端 未结 9 1589
萌比男神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:37

    Converting data from a String to a StringBuilder and back will take some time. If one is only performing a single replace operation, this time may not be recouped by the efficiency improvements inherent in StringBuilder. On the other hand, if one converts a string to a StringBuilder, then performs many Replace operations on it, and converts it back at the end, the StringBuilder approach is apt to be faster.

提交回复
热议问题