如何进行不区分大小写的字符串比较?

发布于 2024-09-07 06:40:00 字数 600 浏览 7 评论 0原文

如何使下面的行不区分大小写?

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

今天早些时候,我得到了一些建议,建议我使用:

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

问题是我无法让它工作,我已经尝试了下面的行,它编译但返回错误的结果,它将注册的用户返回为未注册的用户,并将未注册的用户返回为已注册。

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

谁能指出问题所在吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

唱一曲作罢 2024-09-14 06:40:23

您始终可以使用函数:
.ToLower();
.ToUpper();

转换你的字符串然后比较它们......

祝你好运

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

convert your strings and then compare them...

Good Luck

暮光沉寂 2024-09-14 06:40:22

使用 StringComparison.CurrentCultureIgnoreCase 来代替怎么样?

How about using StringComparison.CurrentCultureIgnoreCase instead?

深陷 2024-09-14 06:40:21

我想您会在此链接中找到更多信息:

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

使用 String 类上的 Compare 静态方法来比较两个字符串。比较是否不区分大小写由其重载之一的第三个参数确定。例如:

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

caseSensitiveResult 值为 -1(表示 lowerCase“小于”upperCase),caseInsensitiveResult 值为 0(表示 lowerCase“等于”upperCase)。

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

梨涡少年 2024-09-14 06:40:20

我想为 EqualsIgnoreCase 编写一个扩展方法

public static class StringExtensions
{
    public static bool? EqualsIgnoreCase(this string strA, string strB)
    {
        return strA?.Equals(strB, StringComparison.CurrentCultureIgnoreCase);
    }
}

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);
    }
}
屋檐 2024-09-14 06:40:18

您可以(尽管有争议)扩展 System.String 以提供不区分大小写的比较扩展方法:

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

并按如下方式使用:

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

C# 允许您创建可以在项目中充当语法糖的扩展方法,非常有用我想说。

这不是答案,我知道这个问题已经很老了并且已经解决了,我只是想添加这些内容。

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.

萌逼全场 2024-09-14 06:40:15

其他人的答案在这里完全有效,但不知何故,输入 StringComparison.OrdinalIgnoreCase 并使用 String.Compare 需要一些时间。

我编写了简单的字符串扩展方法,您可以在其中指定比较是区分大小写还是使用布尔值无大小写,在此处附加整个代码片段:

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;
    }
}

在整个比较大约缩短 10 个字符之后 - 比较:

在使用字符串扩展之前:

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

在使用字符串之后扩大:

testFilename.CompareTo(testToStart, true)

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)
待"谢繁草 2024-09-14 06:40:13

您应该使用静态 String.Compare 函数,如下所示

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

You should use static String.Compare function like following

x => String.Compare (x.Username, (string)drUser["Username"],
                     StringComparison.OrdinalIgnoreCase) == 0
丘比特射中我 2024-09-14 06:40:11

请使用此进行比较:

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

Please use this for comparison:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
冰雪之触 2024-09-14 06:40:08

这不是 .NET Framework(4 和 +)中检查相等性的最佳实践

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

请改用以下内容

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

MSDN 推荐:

  • 使用 String.Equals 方法的重载来测试两个字符串是否相等。
  • 使用String.CompareString.CompareTo方法对字符串进行排序,不检查相等性

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文