Check if the string contains one of 10 characters

I am using C # and I want to check if a string contains one of ten characters, *, &, #, etc.

What is the best way?

+97
string c #
Sep 07 '09 at 19:51
source share
6 answers

The following would be the simplest method, in my opinion:

var match = str.IndexOfAny(new char[] { '*', '&', '#' }) != -1 

Or perhaps in a readable form:

 var match = str.IndexOfAny("*&#".ToCharArray()) != -1 

Depending on the required context and performance, you may or may not want to cache the char array.

+196
Sep 07 '09 at 19:54
source share

As others have said, use IndexOfAny. However, I would use it this way:

 private static readonly char[] Punctuation = "*&#...".ToCharArray(); public static bool ContainsPunctuation(string text) { return text.IndexOfAny(Punctuation) >= 0; } 

This way you will not create a new array for each call. A string is also easier to scan than a series of character literals, IMO.

Of course, if you are going to use this only once, then the lost creation is not a problem, you can use:

 private const string Punctuation = "*&#..."; public static bool ContainsPunctuation(string text) { return text.IndexOfAny(Punctuation.ToCharArray()) >= 0; } 

or

 public static bool ContainsPunctuation(string text) { return text.IndexOfAny("*&#...".ToCharArray()) >= 0; } 

Actually, it depends on what you find more readable, whether you want to use punctuation marks elsewhere and how often this method will be called.




EDIT: Here's an alternative to the Reed Copsi method to find out if a string contains only one of the characters.

 private static readonly HashSet<char> Punctuation = new HashSet<char>("*&#..."); public static bool ContainsOnePunctuationMark(string text) { bool seenOne = false; foreach (char c in text) { // TODO: Experiment to see whether HashSet is really faster than // Array.Contains. If all the punctuation is ASCII, there are other // alternatives... if (Punctuation.Contains(c)) { if (seenOne) { return false; // This is the second punctuation character } seenOne = true; } } return seenOne; } 
+38
Sep 07 '09 at 19:57
source share

If you just want to see if it contains any character, I would recommend using string.IndexOfAny, as suggested elsewhere.

If you want to check that a string contains exactly one of ten characters and only one, then this gets a little more complicated. I believe that the fastest way would be to check for intersection and then check for duplicates.

 private static char[] characters = new char [] { '*','&',... }; public static bool ContainsOneCharacter(string text) { var intersection = text.Intersect(characters).ToList(); if( intersection.Count != 1) return false; // Make sure there is only one character in the text // Get a count of all of the one found character if (1 == text.Count(t => t == intersection[0]) ) return true; return false; } 
+5
Sep 07 '09 at 20:02
source share

string.IndexOfAny (...)

+3
Sep 07 '09 at 19:54
source share
 var specialChars = new[] {'\\', '/', ':', '*', '<', '>', '|', '#', '{', '}', '%', '~', '&'}; foreach (var specialChar in specialChars.Where(str.Contains)) { Console.Write(string.Format("string must not contain {0}", specialChar)); } 
+1
Jul 07 2018-11-11T00:
source share

Thanks everyone! (And basically John!): This allowed me to write the following:

  private static readonly char[] Punctuation = "$€£".ToCharArray(); public static bool IsPrice(this string text) { return text.IndexOfAny(Punctuation) >= 0; } 

as I was looking for a good way to determine if a particular line was really a price or offer, such as "Too little to display."

0
May 08 '13 at 16:39
source share



All Articles