如何使用Regex.Replace一次性替换两个字符串?

编程入门 行业动态 更新时间:2024-10-25 10:26:53
本文介绍了如何使用Regex.Replace一次性替换两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下的方法是从文件名称更换咚咚的迹象,但我希望也能在同一时间,以取代单引号'。我该怎么办呢? 这是文件名的值=提供license_A'R_Ab#海龙Settlements_1-11-09.xls

静态字符串removeBadCharPound(字符串文件名) {//用_字符替换无效字符。 //我想是这样的,但是不工作 //返回Regex.Replace(文件名,#,_); 返回Regex.Replace(文件名,#,_); }

解决方案

尝试

返回Regex.Replace(文件名,[#'],_);

你要知道,我不知道,一个正则表达式可能比有些简单速度快:

返回filename.Replace('#','_') .Replace('\'' _);

I have the following method that is replacing a "pound" sign from the file name but I want also to be able to replace the "single apostrophe ' " at the same time. How can I do it? This is the value of filename =Provider license_A'R_Ab#acus Settlements_1-11-09.xls

static string removeBadCharPound(string filename) { // Replace invalid characters with "_" char. //I want something like this but is NOT working //return Regex.Replace(filename, "# ' ", "_"); return Regex.Replace(filename, "#", "_"); }

解决方案

Try

return Regex.Replace(filename, "[#']", "_");

Mind you, I'm not sure that a regex is likely to be faster than the somewhat simpler:

return filename.Replace('#', '_') .Replace('\'', '_');

更多推荐

如何使用Regex.Replace一次性替换两个字符串?

本文发布于:2023-11-04 00:48:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   两个   Replace   Regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!