我应该使用哪个类来在字符串中进行多次替换?
我必须进行大量文本替换。哪个类最适合以高性能方式实现这一点?是字符串生成器吗?
StringBuilder stringBuilder=new StringBuilder(startString);
stringBuilder.Replace(literala1,literala2);
stringBuilder.Replace(literalb1,literalb2);
stringBuilder.Replace(literalc1,literalc2);
...
或者有更好的课程可以做到这一点吗?顺便说一句,文字大部分都是常量。
I have to make a lot of text-replacements. Which class is best used to make this in a performant manner? Is it StringBuilder?
StringBuilder stringBuilder=new StringBuilder(startString);
stringBuilder.Replace(literala1,literala2);
stringBuilder.Replace(literalb1,literalb2);
stringBuilder.Replace(literalc1,literalc2);
...
or is there a better class to do this? By the way, the literals will be mostly constants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Roberto Farah 的博客详细讨论了这个问题:比较 RegEx.Replace、String.Replace 和 StringBuilder.Replace – 哪个性能更好?
我将在此总结研究结果,这让许多 .NET 开发人员感到震惊。事实证明,对于相对简单的字符串替换(在匹配不需要区分大小写的情况下),
RegEx.Replace()
的性能最差,RegEx.Replace()
的性能最差, >String.Replace()
以最好的获胜。还提供了 CodeProject 上一篇文章的链接,该文章证实了这些发现:StringBuilder vs String / Fast String Operations with .NET 2.0
一般来说,我认为规则应该如下:
String.Replace()
StringBuilder.Replace()
替换数RegEx.Replace
),在这种情况下,为了处理所有必要的替换的单个表达式的优雅性而付出轻微的性能损失是值得的。This exact question was dealt with at length on Roberto Farah's blog: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?
I'll summarize the findings here, which come as a shock to many .NET developers. It turns out that for relatively simple string replacement (in cases where it's not necessary for matches to be case sensitive),
RegEx.Replace()
has the worst performance andString.Replace()
wins with the best.A link is also provided to an article on CodeProject that confirms these findings: StringBuilder vs String / Fast String Operations with .NET 2.0
In general, I would say the rules ought to be as follows:
String.Replace()
when you only have to do a small number of replacements (say around 5)StringBuilder.Replace()
when you have to do a larger number of replacementsRegEx.Replace
) only for the most complex scenarios where it's worth paying a slight performance penalty for the elegance of a single expression that handles all of the necessary replacements.我会选择 RegEx.Replace。此重载: http://msdn.microsoft.com/en-us/library/ cft8645c.aspx
您的所有不同输入都可以在正则表达式中进行匹配,并且所有不同的替换字符串都可以放入您的 MatchEvaluator 中。
I would go with RegEx.Replace. This overload: http://msdn.microsoft.com/en-us/library/cft8645c.aspx
All your different inputs can be matched in the regular expression and all your different replacements strings could go in your MatchEvaluator.
StringBuilder
可能是执行此操作的最佳类,因为它在替换期间不会创建底层字符缓冲区的额外副本。如果您对性能敏感,那么String
可能会不好,因为它会在每次调用Replace
时创建字符串的副本,并使用Regex
可能不如StringBuilder
的直接搜索和替换。StringBuilder
is probably the best class for doing this, as it won't create extra copies of the underlying character buffer during replacements. If you are performance-sensitive, thenString
may be bad because it creates copies of the string with every call toReplace
, and using aRegex
will probably be inferior to the straightforward search-and-replace ofStringBuilder
.我发现使用 此代码实现 Aho-Corasick 字符串匹配 来查找所有字符串匹配,然后只使用 StringBuilder 进行一次替换,这比每次循环一组字符串替换要好得多。
I found using this code implementing Aho-Corasick string matching to find all the strings to match and then only going your string only once with StringBuilder doing the replacements was a lot better than looping with a set of string replacements one at a time.