Is there a C# case insensitive equals operator?

I know that the following is case sensitive:

if (StringA == StringB) {

So is there an operator which will compare two strings in an insensitive manner?

Try this:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);

The best way to compare 2 strings ignoring the case of the letters is to use the String.Equals static method specifying an ordinal ignore case string comparison. This is also the fastest way, much faster than converting the strings to lower or upper case and comparing them after that.

I tested the performance of both approaches and the ordinal ignore case string comparison was more than 9 times faster! It is also more reliable than converting strings to lower or upper case (check out the Turkish i problem). So always use the String.Equals method to compare strings for equality:

String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);

If you want to perform a culture specific string comparison you can use the following code:

String.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase);

Please note that the second example uses the the string comparison logic of the current culture, which makes it slower than the “ordinal ignore case” comparison in the first example, so if you don’t need any culture specific string comparison logic and you are after maximum performance, use the “ordinal ignore case” comparison.

For more information, read the full story on my blog.

There are a number of properties on the StringComparer static class that return comparers for any type of case-sensitivity you might want:

StringComparer Properties

For instance, you can call

StringComparer.CurrentCultureIgnoreCase.Equals(string1, string2)


StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)

It’s a bit cleaner than the string.Equals or string.Compare overloads that take a StringComparison argument.




string.Equals(StringA, StringB, StringComparison.CurrentCultureIgnoreCase);


if (StringA.Equals(StringB, StringComparison.CurrentCultureIgnoreCase)) {

but you need to be sure that StringA is not null. So probably better tu use:

string.Equals(StringA , StringB, StringComparison.CurrentCultureIgnoreCase);

as John suggested

EDIT: corrected the bug

You can use

if (stringA.equals(StringB, StringComparison.CurrentCultureIgnoreCase))

Operator? NO, but I think you can change your culture so that string comparison is not case-sensitive.

// you'll want to change this...
// and you'll want to custimize this

I’m confident that it will change the way that strings are being compared by the equals operator.

Here an idea to simplify the syntax:

public class IgnoreCase
    private readonly string _value;

    public IgnoreCase(string s)
        _value = s;

    protected bool Equals(IgnoreCase other)
        return this == other;

    public override bool Equals(object obj)
        return obj != null &&
               (ReferenceEquals(this, obj) || (obj.GetType() == GetType() && this == (IgnoreCase) obj));

    public override int GetHashCode()
        return _value?.GetHashCode() ?? 0;

    public static bool operator ==(IgnoreCase a, IgnoreCase b)
        return string.Equals(a, b, StringComparison.OrdinalIgnoreCase);

    public static bool operator !=(IgnoreCase a, IgnoreCase b)
        return !(a == b);

    public static implicit operator string(IgnoreCase s)
        return s._value;

    public static implicit operator IgnoreCase(string s)
        return new IgnoreCase(s);

Usable like:

Console.WriteLine((IgnoreCase) "a" == "b"); // false
Console.WriteLine((IgnoreCase) "abc" == "abC"); // true
Console.WriteLine((IgnoreCase) "Abc" == "aBc"); // true
Console.WriteLine((IgnoreCase) "ABC" == "ABC"); // true

I am so used to typing at the end of these comparison methods: , StringComparison.

So I made an extension.

namespace System
{   public static class StringExtension
        public static bool Equals(this string thisString, string compareString,
             StringComparison stringComparison)
            return string.Equals(thisString, compareString, stringComparison);

Just note that you will need to check for null on thisString prior to calling the ext.

string.Compare(string1, string2, true)

if (StringA.ToUpperInvariant() == StringB.ToUpperInvariant()) {

People report ToUpperInvariant() is faster than ToLowerInvariant().

Others answer are totally valid here, but somehow it takes some time to type StringComparison.OrdinalIgnoreCase and also using String.Compare.

I’ve coded simple String extension method, where you could specify if comparison is case sensitive or case senseless with boolean – see following answer:

//You can make it case insensitive by
s1.ToLower() == s2.ToLower();

Leave a Comment