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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/631233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:04:38  来源:igfitidea点击:

Is there a C# case insensitive equals operator?

c#.netstringoperatorscase-insensitive

提问by GateKiller

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?

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

采纳答案by John Feminella

Try this:

尝试这个:

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

回答by leppie

System.Collections.CaseInsensitiveComparer

or

或者

System.StringComparer.OrdinalIgnoreCase

回答by John Leidegren

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.

我相信它会改变 equals 运算符比较字符串的方式。

回答by Erick

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

回答by knoopx

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

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

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

回答by Andy Mikula

You can use

您可以使用

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

回答by Grzenio

or

或者

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

but you need to be sure that StringA is not null. So probably better tu use:

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

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

as John suggested

正如约翰所建议的

EDIT: corrected the bug

编辑:更正了错误

回答by user25623

string.Compare(string1, string2, true)

回答by Ryan Lundy

There are a number of properties on the StringComparerstatic class that return comparers for any type of case-sensitivity you might want:

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

StringComparerProperties

StringComparer特性

For instance, you can call

例如,您可以调用

StringComparer.CurrentCultureIgnoreCase.Equals(string1, string2)

or

或者

StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)

It's a bit cleaner than the string.Equalsor string.Compareoverloads that take a StringComparisonargument.

它比带参数的string.Equalsorstring.Compare重载更简洁一些StringComparison

回答by Valamas

I am so used to typing at the end of these comparison methods: , StringComparison.

我已经习惯了在这些比较方法的末尾打字: , 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 thisStringprior to calling the ext.

请注意,您需要thisString在调用 ext 之前检查空值。