张赐荣,视障者,信息无障碍专家
深耕Web/PC/移动端可访问性研究与实践工作多年,对跨平台无障碍解决方案拥有深刻的独特理论和丰富的实战经验。
精通视障用户软件交互设计,致力于用专业的能力改善、提升产品可及性体验。

張賜榮

张赐荣的技术博客

博客园 首页 新随笔 联系 订阅 管理

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 2022-02-16 13:26  张赐荣  阅读(832)  评论(0)    收藏  举报

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