我应该使用哪个类来在字符串中进行多次替换?

发布于 2024-10-12 06:26:28 字数 310 浏览 3 评论 0原文

我必须进行大量文本替换。哪个类最适合以高性能方式实现这一点?是字符串生成器吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生一梦 2024-10-19 06:26:28

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

一般来说,我认为规则应该如下:

  • 当您只需进行少量替换(例如 5 左右)时,请使用 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 and String.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:

  • Use String.Replace() when you only have to do a small number of replacements (say around 5)
  • Use StringBuilder.Replace() when you have to do a larger number of replacements
  • Reserve regular expressions (RegEx.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.
  • Ignore all of the above guidelines and use whatever makes your code most readable or expressive. Prematurely optimizing something like this isn't worth the time it took me to write this answer.
网白 2024-10-19 06:26:28

我会选择 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.

悟红尘 2024-10-19 06:26:28

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, then String may be bad because it creates copies of the string with every call to Replace, and using a Regex will probably be inferior to the straightforward search-and-replace of StringBuilder.

太傻旳人生 2024-10-19 06:26:28

我发现使用 此代码实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文