Fastest way to replace multiple strings in a huge string

后端 未结 8 721
野的像风
野的像风 2020-12-13 04:51

I m looking for the fastest way to replace multiple (~500) substrings of a big (~1mb) string. Whatever I have tried it seems that String.Replace is the fastest way of doing

8条回答
  •  爱一瞬间的悲伤
    2020-12-13 05:08

    As your input string can be as long as 2Mb, I don't foresee any memory allocation problem. You can load everything in memory and replace your data.

    If from BC you ALWAYS needs to replace for AA, a String.Replace will be ok. But, if you need more control, you could use a Regex.Replace:

    var input  = "ABCDABCABCDABCABCDABCABCDABCD";
    var output = Regex.Replace(input, "BC", (match) =>
    {
        // here you can add some juice, like counters, etc
        return "AA";
    });
    

提交回复
热议问题