by @CodingExample

How to check if String contains case insensitive in C#

Home » c sharp » How to check if String contains case insensitive in C#

We can check if a string contains a substring which is case insensitive by using the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use.

Example:

string title = "How are YOU";
bool contains = title.IndexOf("you", StringComparison.OrdinalIgnoreCase) >= 0;

Defining CaseInsenstive method

We can also define our CaseInsenstive method like this.

public static class Extensions
{
    public static bool CaseInsensitive(this string text, string value,
        StringComparison stringComparison = StringComparison.CurrentCultureIgnoreCase)
    {
        return text.IndexOf(value, stringComparison) >= 0;
    }
}

Usage:

string title = "How are YOU";
bool contains = title.CaseInsensitive('you');