string str1 = "12345ABC...\\...ABC100000";
// Hypothetically huge string of 100000 + Unicode Chars
str1 = str1.Replace("1", string.Empty);
str1 = str1.Replace("22", string.Empty);
str1 = str1.Replace("656", string.Empty);
str1 = str1.Replace("77ABC", string.Empty);
// ... this replace anti-pattern might happen with upto 50 consecutive lines of code.
str1 = str1.Replace("ABCDEFGHIJD", string.Empty);
Run Code Online (Sandbox Code Playgroud)
我继承了一些与上面的代码片段相同的代码.它需要一个巨大的字符串,并从大字符串中替换(删除)常量较小的字符串.
我相信这是一个非常耗费内存的过程,因为每个替换都会在内存中分配新的大型不可变字符串,等待通过GC死亡.
1.更换这些值的最快方法是什么,忽略内存问题?
2.实现相同结果的最有效的内存方式是什么?
我希望这些是相同的答案!
在这些目标之间适合某些地方的实用解决方案也值得赞赏.
假设:
我有一堆lat/long对作为字符串:
-94.5555,95.5555
我正在使用的API需要切换它们:
95.555,-94.555
这样做有什么光滑,简短的方法吗?