Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a case insensitive string comparison?

How can I make the line below case insensitive?

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

I was given some advice earlier today that suggested I use:

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

the trouble is I can't get this to work, I've tried the line below, this compiles but returns the wrong results, it returns enrolled users as unenrolled and unenrolled users as enrolled.

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

Can anyone point out the problem?

like image 796
Jamie Avatar asked Jun 25 '10 22:06

Jamie


People also ask

Which will do case-insensitive comparison of string contents?

The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the i flag.

How do you compare case sensitive?

To perform a case-sensitive comparison of two char variables, simply use the Equals method, which, by default, performs a case-sensitive comparison. Performing a case-insensitive comparison requires that both characters be converted to their uppercase values (they could ...

How do you do case-insensitive string comparison in C++?

Case-insensitive string comparison in C++ Here the logic is simple. We will convert the whole string into lowercase or uppercase strings, then compare them, and return the result. We have used the algorithm library to get the transform function to convert the string into lowercase string.


8 Answers

This is not the best practice in .NET framework (4 & +) to check equality

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

Use the following instead

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

MSDN recommends:

  • Use an overload of the String.Equals method to test whether two strings are equal.
  • Use the String.Compare and String.CompareTo methods to sort strings, not to check for equality.
like image 108
ocean4dream Avatar answered Sep 28 '22 20:09

ocean4dream


You should use static String.Compare function like following

x => String.Compare (x.Username, (string)drUser["Username"],
                     StringComparison.OrdinalIgnoreCase) == 0
like image 40
Oleg Avatar answered Sep 28 '22 19:09

Oleg


Please use this for comparison:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
like image 36
Gautam Kumar Sahu Avatar answered Sep 28 '22 20:09

Gautam Kumar Sahu


You can (although controverse) extend System.String to provide a case insensitive comparison extension method:

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

and use as such:

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

C# allows you to create extension methods that can serve as syntax suggar in your project, quite useful I'd say.

It's not the answer and I know this question is old and solved, I just wanted to add these bits.

like image 37
Felype Avatar answered Sep 28 '22 21:09

Felype


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, attaching whole code snippet 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 whole comparison shortens by 10 characters approximately - compare:

Before using String extension:

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

After using String extension:

testFilename.CompareTo(testToStart, true)
like image 31
TarmoPikaro Avatar answered Sep 28 '22 19:09

TarmoPikaro


I'd like to write an extension method for EqualsIgnoreCase

public static class StringExtensions
{
    public static bool? EqualsIgnoreCase(this string strA, string strB)
    {
        return strA?.Equals(strB, StringComparison.CurrentCultureIgnoreCase);
    }
}
like image 42
Ranga Avatar answered Sep 28 '22 20:09

Ranga


I think you will find more information in this link:

http://codeidol.com/community/dotnet/controlling-case-sensitivity-when-comparing-two-st/8873/

Use the Compare static method on the String class to compare the two strings. Whether the comparison is case-insensitive is determined by the third parameter of one of its overloads. For example:

string lowerCase = "abc";
string upperCase = "AbC";
int caseInsensitiveResult = string.Compare(lowerCase, upperCase,
  StringComparison.CurrentCultureIgnoreCase);
int caseSensitiveResult = string.Compare(lowerCase,
  StringComparison.CurrentCulture);

The caseSensitiveResult value is -1 (indicating that lowerCase is "less than" upperCase) and the caseInsensitiveResult is zero (indicating that lowerCase "equals" upperCase).

like image 31
I_Al-thamary Avatar answered Sep 28 '22 21:09

I_Al-thamary


How about using StringComparison.CurrentCultureIgnoreCase instead?

like image 21
decyclone Avatar answered Sep 28 '22 20:09

decyclone