张赐荣——一位视障程序员。
赐荣小站: www.prc.cx

張賜榮

张赐荣的技术博客

博客园 首页 新随笔 联系 订阅 管理
  92 随笔 :: 27 文章 :: 2 评论 :: 77074 阅读

C# .NET类库自带的str.Replace() 方法替换文本不能区分大小写。
我们可以自己编写一个扩展方法,支持文本忽略大小写替换。
以下扩展方法实现了使用正则表达式忽略大小写替换文本。

public static string ReplaceText(this string text, string find, string replacement, bool caseSensitive = false, int startAt = 0, int count = -1) // 正则替换字符串
{
try
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(System.Text.RegularExpressions.Regex.Escape(find), (caseSensitive ? System.Text.RegularExpressions.RegexOptions.None : System.Text.RegularExpressions.RegexOptions.IgnoreCase));
return (regex.Replace(text, replacement, count, startAt));
}
catch (Exception)
{
return (text);
}
}  

posted on   张赐荣  阅读(785)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

感谢访问张赐荣的技术分享博客!
博客地址:https://cnblogs.com/netlog/
知乎主页:https://www.zhihu.com/people/tzujung-chang
个人网站:https://prc.cx/

点击右上角即可分享
微信分享提示