开发者

Total no of possible special characters can be used in C#? [duplicate]

This question already has answers here: 开发者_如何学运维 Closed 12 years ago.

Possible Duplicate:

How to find whether a string contains any of the special characters?

Dear All,

simple question I have a string and I want to make sure that there are special characters (like # $ _ & %) in that string.How can we achieve this using C#

Thanks lokesh


Use String.IndexOfAny: http://msdn.microsoft.com/en-us/library/system.string.indexofany.aspx

bool specialCharacterIsInString = myString.IndexOfAny(new char[]{'#', '$', '_', '&', '%'}) != -1;

Adjust the characters array so that contains the characters that you consider "special character" in your current context.


You can also use Linq and Char.IsLetterOrDigit:

bool hasSymbol = myString.Any(!char.IsLetterOrDigit(x));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜