如果字符串包含特殊字符,如何测试它 [英] how to test a string if it contains special characters

查看:77
本文介绍了如果字符串包含特殊字符,如何测试它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试字符串以查看它是否包含特殊字符?我想要确保在我的表格中输入的任何名字只有字母(而且

可能允许短划线和撇号)。


我可以循环RealName.Contains(" ..."),但必须有一个更优雅的

解决方案。

How do you test a string to see if it contains special characters? I
want to ensure that any names typed into my form has only letters (and
maybe allow a dash and an apostrophe).

I can loop RealName.Contains("..."), but there must be a more elegant
solution.

推荐答案

" titan nyquist" < ti *********** @ gmail.com写信息

新闻:11 ******************* ***@q69g2000hsb.googlegr oups.com ...
"titan nyquist" <ti***********@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...

如何测试字符串以查看它是否包含特殊字符?我想要确保在我的表格中输入的任何名字只有字母(而且

可能允许短划线和撇号)。


我可以循环RealName.Contains(" ..."),但必须有一个更优雅的

解决方案。
How do you test a string to see if it contains special characters? I
want to ensure that any names typed into my form has only letters (and
maybe allow a dash and an apostrophe).

I can loop RealName.Contains("..."), but there must be a more elegant
solution.



你可以使用正则表达式:

You can use a Regular Expression:

RegEx re = new RegEx("^[-''a-zA-Z]*


");

if(re.IsMatch(stringToTest)).... //字符串是否正确
");
if (re.IsMatch(stringToTest)) ....//string is correct


谢谢!

您可以使用正则表达式:


RegEx re =新RegEx(" ^ [ - ''a-zA-Z] *
You can use a Regular Expression:

RegEx re = new RegEx("^[-''a-zA-Z]*


这篇关于如果字符串包含特殊字符,如何测试它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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