如何从我的字符串删除多个违规字符? [英] How do I remove multiple offending characters from my string?

查看:134
本文介绍了如何从我的字符串删除多个违规字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我工作的代码:

string Input;
string Output;

Input = data;
Output = Input.Replace(@")", "");



在这里,我只是从我的字符串去掉括号),如果它存在。现在,我怎么得罪扩展字符一样)包含的名单(和 - ?以及

Here, I am simply removing the parentheses ")" from my string, if it exists. Now how do I expand the list of offending characters like ")" to include "(" and "-" as well?

我知道我可以写2个输出 - 类似声明,但我不知道是否有更好的方法...

I realize I can write 2 more Output-like statements, but I'm wondering if there is a better way...

推荐答案

如果你只是在做一对夫妇替代品(我看你只是做了三),无需担心正则表达式或StringBuilders最简单的方法就是链的三家调用替换成一个语句:

If you're just doing a couple replacements (I see you're only doing three), the easiest way without worrying about Regex or StringBuilders is to chain three Replace calls into one statement:

Output = Input.Replace("(", "").Replace(")", "").Replace("-", "");

... ...这是略高于每一次结果存储在输出更好。

... which is marginally better than storing the result in Output every time.

这篇关于如何从我的字符串删除多个违规字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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