是否有替代与string.replace是不区分大小写? [英] Is there an alternative to string.Replace that is case-insensitive?
问题描述
我需要搜索的字符串和替换%名字%
和%PolicyAmount%
中所有出现一个值从数据库中抽取。问题是名字的大小写不同而不同。这$ P $使用该与string.replace()
方法pvents我。我已经看到了关于这个问题的web页面提示
I need to search a string and replace all occurances of %FirstName%
and %PolicyAmount%
with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace()
method. I've seen web pages on the subject that suggest
Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase);
不过,对于当我尝试用 $ 1,0
替换%PolicyAmount%
某些原因,更换从未发生。我认为它是与美元符号是在正则表达式保留字符。
However for some reason when I try and replace %PolicyAmount%
with $0
, the replacement never takes place. I assume that it has something to do with the dollar sign being a reserved character in regex.
有另一种方法,我可以使用,不涉及消毒的输入,处理正则表达式的特殊字符?
Is there another method I can use that doesn't involve sanitizing the input to deal with regex special characters?
推荐答案
从MSDN
$ 0 - 替代匹配的组号编号(十进制)的最后一个子
From MSDN
$0 - "Substitutes the last substring matched by group number number (decimal)."
在.NET正EX pressions组0总是整场比赛。对于文字$需要
In .NET Regular expressions group 0 is always the entire match. For a literal $ you need to
string value = Regex.Replace("%PolicyAmount%", "%PolicyAmount%", @"$$0", RegexOptions.IgnoreCase);
这篇关于是否有替代与string.replace是不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!