在一个大的文本模板替换标记的最佳方法 [英] Best way to replace tokens in a large text template
问题描述
我有,需要由其他替代文本标记过的部分大型文本模板。这些标记是这个样子:## USERNAME ##。我的第一反应就是用与string.replace(),但有一个更好的,更有效的方法还是更换()已经为此进行了优化?
I have a large text template which needs tokenized sections replaced by other text. The tokens look something like this: ##USERNAME##. My first instinct is just to use String.Replace(), but is there a better, more efficient way or is Replace() already optimized for this?
推荐答案
System.Text.RegularExpressions.Regex.Replace ()是你追求的东西 - 如果你的令牌是够奇怪的,你需要一个正则表达式来找到他们。
System.Text.RegularExpressions.Regex.Replace() is what you seek - IF your tokens are odd enough that you need a regex to find them.
的Some样的灵魂做了一些性能测试和Regex.Replace(),与string.replace()和StringBuilder.Replace(),与string.replace()之间实际上是技高一筹。
Some kind soul did some performance testing, and between Regex.Replace(), String.Replace(), and StringBuilder.Replace(), String.Replace() actually came out on top.
这篇关于在一个大的文本模板替换标记的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!