【问题标题】:how to replace a string ignoring case? [duplicate]如何替换忽略大小写的字符串? [复制]
【发布时间】:2012-01-21 04:54:00
【问题描述】:

可能重复:
Is there an alternative to string.Replace that is case-insensitive?

我正在寻找替代方案:

string str = "data ... ";
string replace = "data";
str = str.Replace(replace, "new value");
str = str.Replace(replace.ToLower(),"new value");

如果可能,不使用正则表达式。 提前致谢。

【问题讨论】:

标签: c# .net string replace


【解决方案1】:

没有正则表达式,我不知道。

使用正则表达式,它会给你这样的东西:

var regex = new Regex( str, RegexOptions.IgnoreCase );
var newSentence = regex.Replace( sentence, "new value" );

我在这里发现了一篇有趣的文章,其中的示例代码看起来比 Regex.Replace 运行得更快:http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    相关资源
    最近更新 更多