Quick and Dirty way to search for characters in a string
- by mbcrump
I saw this today on StackOverflow, all credit goes to Jon Skeet. I have always used RegEx or loops to check for special chars. This is a more human-friendly way of doing it. Code Snippet using System; class Program { //field private static readonly char[] SpecialChars = "!@#$%^&*()".ToCharArray(); static void Main(string[] args) { //text to search for string text = "michael"; int indexOf = text.IndexOfAny(SpecialChars); if (indexOf == -1) { Console.WriteLine("no special char"); } Console.ReadLine(); } }