如何在C#中检查输入字符串中的特殊字符 [英] How to check special characters in input string in C#
本文介绍了如何在C#中检查输入字符串中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
如何使用正则表达式检查输入字符串是否只包含特殊字符。
Hi
How to check the input string contain only special characters ,using regular expression.
public static bool IsSpecialCharacters(this string stringToTest)
{
const string charSet = "[^a-z0-9]";
//Regex RgxUrl = new Regex("[^a-z0-9]");
//blnContainsSpecialCharacters = RgxUrl.IsMatch(stringToCheck);
return Regex.Match(stringToTest, @"^[" + charSet + @"]+$").Success;
}
此方法是否正确?
Is this method is correct?
推荐答案
)。成功;
}
").Success; }
这种方法是否正确?
Is this method is correct?
你可以简单地检查如下。
只需添加命名空间
使用系统。 linq
int PasswordSpecialChar = txtMonth.Text.Count(p =>!char.IsLetterOrDigit(p));
you can simply check like below.
just add namespace
using system.linq
int PasswordSpecialChar = txtMonth.Text.Count(p => !char.IsLetterOrDigit(p));
没有。
例如,您的解决方案不适用于大写字母。试试这个:
No.
Your solution will not work with upper case, for example. Try this:
return Regex.Match(stringToTest, @"^\W+
这篇关于如何在C#中检查输入字符串中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文