Is there a C# case insensitive equals operator?

C#.NetStringOperatorsCase Insensitive

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Try this:

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

Solution 2 - C#

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.

Solution 3 - C#

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)

or

StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)

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

Solution 4 - C#

System.Collections.CaseInsensitiveComparer

or

System.StringComparer.OrdinalIgnoreCase

Solution 5 - C#

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

Solution 6 - C#

or

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

Solution 7 - C#

You can use

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

Solution 8 - C#

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

// you'll want to change this...
System.Threading.Thread.CurrentThread.CurrentCulture
// and you'll want to custimize this
System.Globalization.CultureInfo.CompareInfo

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

Solution 9 - C#

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

Solution 10 - C#

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.

Solution 11 - C#

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

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

Solution 12 - C#

string.Compare(string1, string2, true)

Solution 13 - C#

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:

https://stackoverflow.com/a/49208128/2338477

Solution 14 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGateKillerView Question on Stackoverflow
Solution 1 - C#John FeminellaView Answer on Stackoverflow
Solution 2 - C#Pavel VladovView Answer on Stackoverflow
Solution 3 - C#Ryan LundyView Answer on Stackoverflow
Solution 4 - C#leppieView Answer on Stackoverflow
Solution 5 - C#ErickView Answer on Stackoverflow
Solution 6 - C#GrzenioView Answer on Stackoverflow
Solution 7 - C#Andy MikulaView Answer on Stackoverflow
Solution 8 - C#John LeidegrenView Answer on Stackoverflow
Solution 9 - C#renouveView Answer on Stackoverflow
Solution 10 - C#ValamasView Answer on Stackoverflow
Solution 11 - C#knoopxView Answer on Stackoverflow
Solution 12 - C#user25623View Answer on Stackoverflow
Solution 13 - C#TarmoPikaroView Answer on Stackoverflow
Solution 14 - C#DjAntony ComancheroView Answer on Stackoverflow