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));
精彩评论