本文翻译自: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();
}

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

  1. js中字符替换函数String.replace()使用技巧

    String.replace( ) 简介 语法: var strings = string.replace(regexp, replacement) regexp :您要执行替换操作的正则表达式,如果 ...

  2. java replaceall函数_JAVA中string.replace和string.replaceAll的区别及用法

    展开全部 JAVA中string.replace()和string.replaceAll()的区别及用法乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),32 ...

  3. 用C#中的string.Replace有错误,无法替换。

    我用的是.net 1.1,在编程要将[Title]替换为"这是标题",用string.Replace替换总是不对:temp.Replace("[Title]", ...

  4. java string replace 重载_关于Java:如何使用replace(char,char)替换字符b的所有实例为空...

    如何使用replace(char,char)将字符" b"的所有实例全部替换为空. 例如: Hambbburger to Hamurger 编辑:有一个约束,我只能使用1.4.2, ...

  5. python replace替换多个字符_关于python:使用string.replace(x,y)替换所有

    我刚刚开始学习python,并希望使用string.replace(x,y). 具体来说,根据字母是否最初大写,将所有内容全部替换为X和x. 例如 John S. Smith - > Xxxx ...

  6. java replace无效_Java String.replace()方法无效的原因及解决方式

    首先我们来看个例子 public class Demo1 { public static void main(String[] args) { String aa="abcd"; ...

  7. string.replace()

    replace replace方法对string进行查找和替换,并返回一个新字符串 string.replace(regexp/substr, replacement) regexp/substr:必 ...

  8. java replaceall函数_java基础—-String中replace和replaceAll方法

    这里面我们分析一下replace与replaceAll方法的差异以及原理. replace各个方法的定义 一.replaceFirst方法 public String replaceFirst(Str ...

  9. JAVA中string.replace()和string.replaceAll()的区别及用法 数据库中[]转义

    JAVA中string.replace()和string.replaceAll()的区别及用法 乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replac ...

最新文章

  1. 揭秘人工智能、机器学习和深度学习的神秘面纱
  2. win10 64位SSDT函数索引动态查找
  3. IT规划宜分步走 忌盲目好大喜功(载)
  4. Oracle truncate table 与 delete tabel的区别(转)
  5. C#的static constructor抛了异常会怎么处理?
  6. html5 心跳效果,css3实现心脏跳动
  7. 处理收到的Stanzas
  8. 免费的分区软件MiniTool Partition Wizard Free
  9. python处理Excel数据串行串列问题
  10. ps如何保存透明图片
  11. 质数合数相关操作python代码合集(比较全面,欢迎补充)
  12. 大数据可视化应用_在数据可视化中应用种族平等意识
  13. 斯坦福 机器学习-第一章监督学习
  14. 技术人员谈管理之帕累托法则(80/20法则)
  15. 怎么关闭惠普暗影精灵OMEN 8的主机灯
  16. css动画与渐变案例,使用动画和渐变做一个背景动态网页
  17. 美版有锁iphone4怎么突然连不上网了
  18. 移动CM201-2机顶盒系统设置apk
  19. 关于Protel 2004 绘制电路原理图——遇到的一些小问题
  20. windows获取文件列表及文件夹结构

热门文章

  1. Android开发之LisitView的图文并排效果实现(源代码分享)
  2. iOS身份证号码识别
  3. Java8 Stream 使用
  4. 算法---------前 K 个高频元素(Java版本)
  5. Ubuntu磁盘分区以及双系统启动顺序修改
  6. android studio大坑 executing external native build for cmake
  7. java打包没有src_maven 打包时,src/main/java目录下的xml等资源文件没有打包进去的问题...
  8. Android selinux权限修改
  9. macOS安装 cocoapods1.9.1失败Failed to build gem native extension
  10. putty-psftp