是否有 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
Is there a C# case insensitive equals operator?
提问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 StringComparer
static class that return comparers for any type of case-sensitivity you might want:
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.Equals
or string.Compare
overloads that take a StringComparison
argument.
它比带参数的string.Equals
orstring.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 thisString
prior to calling the ext.
请注意,您需要thisString
在调用 ext 之前检查空值。