How to make String.Contains case insensitive?

CJ7 picture CJ7 · Jul 10, 2013 · Viewed 158.5k times · Source

How can I make the following case insensitive?

myString1.Contains("AbC")

Answer

Tobia Zambon picture Tobia Zambon · Jul 10, 2013

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);