C# 有不区分大小写的等于运算符吗?

发布于 2024-07-14 18:24:15 字数 107 浏览 6 评论 0原文

我知道以下内容区分大小写:

if (StringA == StringB) {

那么是否有一个运算符会以不敏感的方式比较两个字符串?

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?

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

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

发布评论

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

评论(13

少女七分熟 2024-07-21 18:24:16

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

我编写了简单的字符串扩展方法,您可以在其中指定比较是区分大小写还是使用布尔值无大小写 - 请参阅以下答案:

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

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

初见你 2024-07-21 18:24:16
if (StringA.ToUpperInvariant() == StringB.ToUpperInvariant()) {

人们报告 ToUpperInvariant() 比 ToLowerInvariant() 更快。

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

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

庆幸我还是我 2024-07-21 18:24:15

尝试这个:

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

Try this:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
阳光①夏 2024-07-21 18:24:15

比较两个字符串(忽略字母大小写)的最佳方法是使用 String.Equals 指定序数忽略大小写字符串比较的静态方法。 这也是最快的方法,比将字符串转换为小写或大写然后再进行比较要快得多。

我测试了这两种方法的性能,序数忽略大小写字符串比较快了 9 倍以上! 它也比将字符串转换为小写或大写更可靠(查看土耳其语 i 问题)。 因此,请始终使用 String.Equals 方法来比较字符串是否相等

String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);

:如果您想要执行特定于区域性的字符串比较,可以使用以下代码:

String.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase);

请注意,第二个示例使用当前区域性的字符串比较逻辑,这使得它比第一个示例中的“序号忽略大小写”比较慢,因此,如果您不需要任何特定于文化的字符串比较逻辑并且您追求最大性能,请使用“序数忽略大小写”比较。

如需了解更多信息,阅读我博客上的完整故事

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.

她说她爱他 2024-07-21 18:24:15

StringComparer 静态类上有许多属性,可返回您可能需要的任何类型的区分大小写的比较器:

StringComparer Properties

例如,您可以调用

StringComparer.CurrentCultureIgnoreCase.Equals(string1, string2)

StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)

它比 string.Equalsstring.Compare 重载,采用 <代码>StringComparison 参数。

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.

人生戏 2024-07-21 18:24:15
System.Collections.CaseInsensitiveComparer

或者

System.StringComparer.OrdinalIgnoreCase
System.Collections.CaseInsensitiveComparer

or

System.StringComparer.OrdinalIgnoreCase
乱世争霸 2024-07-21 18:24:15
string.Equals(StringA, StringB, StringComparison.CurrentCultureIgnoreCase);
string.Equals(StringA, StringB, StringComparison.CurrentCultureIgnoreCase);
滴情不沾 2024-07-21 18:24:15

或者

if (StringA.Equals(StringB, StringComparison.CurrentCultureIgnoreCase)) {

但您需要确保 StringA 不为空。 所以可能更好地使用:

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

正如约翰建议的

编辑:纠正了错误

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

深爱不及久伴 2024-07-21 18:24:15

您可以使用

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

You can use

if (stringA.equals(StringB, StringComparison.CurrentCultureIgnoreCase))
蓝眸 2024-07-21 18:24:15

操作员? 不,但我认为您可以更改您的文化,以便字符串比较不区分大小写。

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

我相信它将改变等于运算符比较字符串的方式。

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.

手长情犹 2024-07-21 18:24:15

这里有一个简化语法的想法:

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

可用如下:

Console.WriteLine((IgnoreCase) "a" == "b"); // false
Console.WriteLine((IgnoreCase) "abc" == "abC"); // true
Console.WriteLine((IgnoreCase) "Abc" == "aBc"); // true
Console.WriteLine((IgnoreCase) "ABC" == "ABC"); // true

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
如果没结果 2024-07-21 18:24:15

我习惯于在这些比较方法的末尾输入:、StringComparison。

所以我做了一个扩展。

namespace System
{   public static class StringExtension
    {
        public static bool Equals(this string thisString, string compareString,
             StringComparison stringComparison)
        {
            return string.Equals(thisString, compareString, stringComparison);
        }
    }
}

请注意,您需要检查 上是否为 null >thisString 在调用 ext 之前。

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.

萤火眠眠 2024-07-21 18:24:15
string.Compare(string1, string2, true)
string.Compare(string1, string2, true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文