How can I do a case insensitive string comparison?

2019-01-02 15:27发布

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?

8条回答
与君花间醉酒
2楼-- · 2019-01-02 15:50

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).

查看更多
几人难应
3楼-- · 2019-01-02 15:53

Please use this for comparison:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
查看更多
笑指拈花
4楼-- · 2019-01-02 15:59

How about using StringComparison.CurrentCultureIgnoreCase instead?

查看更多
萌妹纸的霸气范
5楼-- · 2019-01-02 16:00

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)
查看更多
春风洒进眼中
6楼-- · 2019-01-02 16:00

you can always use functions: .ToLower(); .ToUpper();

convert your strings and then compare them...

Good Luck

查看更多
梦醉为红颜
7楼-- · 2019-01-02 16:02

You should use static String.Compare function like following

x => String.Compare (x.Username, (string)drUser["Username"],
                     StringComparison.OrdinalIgnoreCase) == 0
查看更多
登录 后发表回答