Coder Perfect

What is the best way to accomplish a case-insensitive string comparison?

Problem

I’m not sure how to make the line below case-insensitive.

drUser["Enrolled"] = 
      (enrolledUsers.FindIndex(x => x.Username == (string)drUser["Username"]) != -1);

I received some suggestions earlier today suggesting that I use:

x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));

The problem is that I can’t get this to work. I tried the line below, which compiles but produces incorrect results: enrolled users are returned as unenrolled, and unenrolled users are returned as enrolled.

drUser["Enrolled"] = 
      (enrolledUsers.FindIndex(x => x.Username.Equals((string)drUser["Username"], 
                                 StringComparison.OrdinalIgnoreCase)));

Is it possible for someone to identify the issue?

Asked by Jamie

Solution #1

In the.NET framework (4 and above), checking equality is not the recommended practice.

String.Compare(x.Username, (string)drUser["Username"], 
                  StringComparison.OrdinalIgnoreCase) == 0

Instead, try the following:

String.Equals(x.Username, (string)drUser["Username"], 
                   StringComparison.OrdinalIgnoreCase) 

MSDN recommends:

Answered by ocean4dream

Solution #2

Static String should be used. Consider the following functions.

x => String.Compare (x.Username, (string)drUser["Username"],
                     StringComparison.OrdinalIgnoreCase) == 0

Answered by Oleg

Solution #3

Please consider the following as a benchmark:

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

Answered by Gautam Kumar Sahu

Solution #4

Other answers are completely valid in this case, but typing StringComparison takes a long time. OrdinalIgnoreCase and String are also used. Compare.

I’ve written a simple String extension method that allows you to define whether the comparison is case sensitive or case insensitive using boolean, and I’ve included the entire code sample here:

using System;

/// <summary>
/// String helpers.
/// </summary>
public static class StringExtensions
{
    /// <summary>
    /// Compares two strings, set ignoreCase to true to ignore case comparison ('A' == 'a')
    /// </summary>
    public static bool CompareTo(this string strA, string strB, bool ignoreCase)
    {
        return String.Compare(strA, strB, ignoreCase) == 0;
    }
}

After that, the entire comparison is reduced by about 10 characters – compare:

Before utilizing the String extension, make the following checks:

String.Compare(testFilename, testToStart,true) != 0

Following the use of the String extension:

testFilename.CompareTo(testToStart, true)

Answered by TarmoPikaro

Solution #5

You can enhance System.String to add a case-insensitive comparison extension function (though this is debatable):

public static bool CIEquals(this String a, String b) {
    return a.Equals(b, StringComparison.CurrentCultureIgnoreCase);
}

as well as the following:

x.Username.CIEquals((string)drUser["Username"]);

C# allows you to construct extension methods that can be used as syntax hints in your project, which I think is extremely handy.

It’s not the answer, and I’m aware that this question has already been answered; I just wanted to offer these details.

Answered by Felype

Post is based on https://stackoverflow.com/questions/3121957/how-can-i-do-a-case-insensitive-string-comparison