如何使用Regex.Replace一次性替换两个字符串? [英] How to use Regex.Replace to Replace Two Strings at Once?

查看:268
本文介绍了如何使用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一次性替换两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆