如何替换字符串忽略大小写? [英] how to replace a string ignoring case?
本文介绍了如何替换字符串忽略大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
是否有替代 string.Replace 的方法不区分大小写?
我正在寻找替代方案:
string str = "data ... ";
string replace = "data";
str = str.Replace(replace, "new value");
str = str.Replace(replace.ToLower(),"new value");
如果可能,不使用正则表达式.提前致谢.
if possible using no regex. Thanks in advance.
推荐答案
没有正则表达式,我不知道.
Without Regex, I don't know.
使用正则表达式,它会给你这样的东西:
With Regex, it would give you something like this :
var regex = new Regex( str, RegexOptions.IgnoreCase );
var newSentence = regex.Replace( sentence, "new value" );
我在这里找到了一篇有趣的文章,其中的示例代码看起来比 Regex.Replace 运行得更快:http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx
I found an interesting article here, with a sample code that looks to work faster than Regex.Replace : http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx
这篇关于如何替换字符串忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文