是否有一个不区分大小写的string.Replace的替代方法?

本文翻译自:Is there an alternative to string.Replace that is case-insensitive?

I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount% with a value pulled from a database. 我需要搜索一个字符串,并将所有出现的%FirstName%%PolicyAmount%替换为从数据库中提取的值。 The problem is the capitalization of FirstName varies. 问题是FirstName的大小写有所不同。 That prevents me from using the String.Replace() method. 这阻止我使用String.Replace()方法。 I've seen web pages on the subject that suggest 我已经看过关于这个主题的网页了

Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase);

However for some reason when I try and replace %PolicyAmount% with $0 , the replacement never takes place. 但是出于某些原因,当我尝试用$0替换%PolicyAmount%时,替换永远不会发生。 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? 是否有其他方法可以使用,不涉及清理输入以处理正则表达式特殊字符?


#1楼

参考:https://stackoom.com/question/11c3/是否有一个不区分大小写的string-Replace的替代方法


#2楼

    /// <summary>
    /// A case insenstive replace function.
    /// </summary>
    /// <param name="originalString">The string to examine.(HayStack)</param>
    /// <param name="oldValue">The value to replace.(Needle)</param>
    /// <param name="newValue">The new value to be inserted</param>
    /// <returns>A string</returns>
    public static string CaseInsenstiveReplace(string originalString, string oldValue, string newValue)
    {
        Regex regEx = new Regex(oldValue,
           RegexOptions.IgnoreCase | RegexOptions.Multiline);
        return regEx.Replace(originalString, newValue);
    }

#3楼

Regex.Replace(strInput, strToken.Replace("$", "[$]"), strReplaceWith, RegexOptions.IgnoreCase);

#4楼

The regular expression method should work. 正则表达式方法应该有效。 However what you can also do is lower case the string from the database, lower case the %variables% you have, and then locate the positions and lengths in the lower cased string from the database. 然而,您还可以做的是小写数据库中的字符串,小写%变量%,然后从数据库中找到下部字符串中的位置和长度。 Remember, positions in a string don't change just because its lower cased. 请记住,字符串中的位置不会因为较低的情况而改变。

Then using a loop that goes in reverse (its easier, if you do not you will have to keep a running count of where later points move to) remove from your non-lower cased string from the database the %variables% by their position and length and insert the replacement values. 然后使用一个反向循环(它更容易,如果你不这样做,你将不得不保持后续点移动到的位置的运行计数)从数据库中删除非低位字符串的%变量%由它们的位置和长度并插入替换值。


#5楼

From MSDN 来自MSDN
$0 - "Substitutes the last substring matched by group number number (decimal)." $ 0 - “替换与组号(十进制)匹配的最后一个子串。”

In .NET Regular expressions group 0 is always the entire match. 在.NET正则表达式中,组0始终是整个匹配。 For a literal $ you need to 对于文字$,你需要

string value = Regex.Replace("%PolicyAmount%", "%PolicyAmount%", @"$$0", RegexOptions.IgnoreCase);

#6楼

Seems like string.Replace should have an overload that takes a StringComparison argument. 看起来像string.Replace 应该有一个带有StringComparison参数的重载。 Since it doesn't, you could try something like this: 既然没有,你可以尝试这样的事情:

public static string ReplaceString(string str, string oldValue, string newValue, StringComparison comparison)
{
    StringBuilder sb = new StringBuilder();

    int previousIndex = 0;
    int index = str.IndexOf(oldValue, comparison);
    while (index != -1)
    {
        sb.Append(str.Substring(previousIndex, index - previousIndex));
        sb.Append(newValue);
        index += oldValue.Length;

        previousIndex = index;
        index = str.IndexOf(oldValue, index, comparison);
    }
    sb.Append(str.Substring(previousIndex));

    return sb.ToString();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值