I want to find whether a string contains any of the special characters like !,@,#,$,%,^,&,*,(,)....etc.
How can I do that without looping thorugh all the characters in the string?
Use String.IndexOfAny:
String.IndexOfAny
private static readonly char[] SpecialChars = "!@#$%^&*()".ToCharArray(); ... int indexOf = text.IndexOfAny(SpecialChars); if (indexOf == -1) { // No special chars }
Of course that will loop internally - but at least you don't have to do it in your code.
1.4m articles
1.4m replys
5 comments
57.0k users