C# 有不区分大小写的等于运算符吗?
我知道以下内容区分大小写:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
其他人的答案在这里完全有效,但不知何故,输入 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 usingString.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
人们报告 ToUpperInvariant() 比 ToLowerInvariant() 更快。
People report ToUpperInvariant() is faster than ToLowerInvariant().
尝试这个:
Try this:
比较两个字符串(忽略字母大小写)的最佳方法是使用 String.Equals 指定序数忽略大小写字符串比较的静态方法。 这也是最快的方法,比将字符串转换为小写或大写然后再进行比较要快得多。
我测试了这两种方法的性能,序数忽略大小写字符串比较快了 9 倍以上! 它也比将字符串转换为小写或大写更可靠(查看土耳其语 i 问题)。 因此,请始终使用 String.Equals 方法来比较字符串是否相等
:如果您想要执行特定于区域性的字符串比较,可以使用以下代码:
请注意,第二个示例使用当前区域性的字符串比较逻辑,这使得它比第一个示例中的“序号忽略大小写”比较慢,因此,如果您不需要任何特定于文化的字符串比较逻辑并且您追求最大性能,请使用“序数忽略大小写”比较。
如需了解更多信息,阅读我博客上的完整故事 。
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:
If you want to perform a culture specific string comparison you can use the following code:
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.
StringComparer
静态类上有许多属性,可返回您可能需要的任何类型的区分大小写的比较器:StringComparer
Properties例如,您可以调用
或
它比
string.Equals
或string.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
PropertiesFor instance, you can call
or
It's a bit cleaner than the
string.Equals
orstring.Compare
overloads that take aStringComparison
argument.或者
or
或者
但您需要确保 StringA 不为空。 所以可能更好地使用:
正如约翰建议的
编辑:纠正了错误
or
but you need to be sure that StringA is not null. So probably better tu use:
as John suggested
EDIT: corrected the bug
您可以使用
You can use
操作员? 不,但我认为您可以更改您的文化,以便字符串比较不区分大小写。
我相信它将改变等于运算符比较字符串的方式。
Operator? NO, but I think you can change your culture so that string comparison is not case-sensitive.
I'm confident that it will change the way that strings are being compared by the equals operator.
这里有一个简化语法的想法:
可用如下:
Here an idea to simplify the syntax:
Usable like:
我习惯于在这些比较方法的末尾输入:
、StringComparison。
所以我做了一个扩展。
请注意,您需要检查
上是否为 null >thisString
在调用 ext 之前。I am so used to typing at the end of these comparison methods:
, StringComparison.
So I made an extension.
Just note that you will need to check for null on
thisString
prior to calling the ext.