Quick and Dirty way to search for characters in a string
Posted
by mbcrump
on Geeks with Blogs
See other posts from Geeks with Blogs
or by mbcrump
Published on Fri, 26 Mar 2010 06:39:45 GMT
Indexed on
2010/03/26
13:43 UTC
Read the original article
Hit count: 379
Filed under:
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();
- }
- }
© Geeks with Blogs or respective owner