How to make String.Contains case insensitive?

How can I make the following case insensitive?

myString1.Contains("AbC") 
+53
string c # case-insensitive
Jul 10 '13 at 6:35
source share
3 answers

You can create your own extension method to do this:

 public static bool Contains(this string source, string toCheck, StringComparison comp) { return source != null && toCheck != null && source.IndexOf(toCheck, comp) >= 0; } 

And then call:

  mystring.Contains(myStringToCheck, StringComparison.OrdinalIgnoreCase); 
+103
Jul 10 '13 at 6:39
source share

You can use:

 if (myString1.IndexOf("AbC", StringComparison.OrdinalIgnoreCase) >=0) { //... } 

This works with any version of .NET.

+37
Jul 10 '13 at 6:38
source share
 bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase); 

[EDIT] extension code:

 public static bool Contains(this string source, string cont , StringComparison compare) { return source.IndexOf(cont, compare) >= 0; } 

That might work :)

+9
Jul 10 '13 at 6:38
source share



All Articles