.net 通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/72696/
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
Which is generally best to use -- StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?
提问by Dave Haynes
I have some code like this:
我有一些这样的代码:
If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then
DoSomething()
End If
I don't care about the case. Should I use OrdinalIgnoreCase, InvariantCultureIgnoreCase, or CurrentCultureIgnoreCase?
我不在乎这个案子。我应该使用OrdinalIgnoreCase, InvariantCultureIgnoreCase, 还是CurrentCultureIgnoreCase?
回答by Robert Taylor
Newer .Net Docs now has a table to help you decide which is best to use in your situation.
较新的 .Net Docs 现在有一个表格来帮助您决定最适合您的情况。
From MSDN's "New Recommendations for Using Strings in Microsoft .NET 2.0"
来自 MSDN 的“在 Microsoft .NET 2.0 中使用字符串的新建议”
Summary: Code owners previously using the
InvariantCulturefor string comparison, casing, and sorting should strongly consider using a new set ofStringoverloads in Microsoft .NET 2.0. Specifically, data that is designed to be culture-agnostic and linguistically irrelevantshould begin specifying overloads using either theStringComparison.OrdinalorStringComparison.OrdinalIgnoreCasemembers of the newStringComparisonenumeration. These enforce a byte-by-byte comparison similar tostrcmpthat not only avoids bugs from linguistic interpretation of essentially symbolic strings, but provides better performance.
简介: 以前使用
InvariantCulturefor 字符串比较、大小写和排序的代码所有者应该强烈考虑String在 Microsoft .NET 2.0 中使用一组新的重载。具体而言,设计为与文化无关且与语言无关的数据应开始使用新枚举的StringComparison.Ordinal或StringComparison.OrdinalIgnoreCase成员指定重载StringComparison。这些强制逐字节比较,strcmp这不仅避免了从本质上符号字符串的语言解释中产生的错误,而且提供了更好的性能。
回答by Sam Saffron
It all depends
这一切都取决于
Comparing unicode strings is hard:
比较 unicode 字符串很困难:
The implementation of Unicode string searches and comparisons in text processing software must take into account the presence of equivalent code points. In the absence of this feature, users searching for a particular code point sequence would be unable to find other visually indistinguishable glyphs that have a different, but canonically equivalent, code point representation.
在文本处理软件中执行 Unicode 字符串搜索和比较必须考虑等效代码点的存在。如果没有此功能,搜索特定代码点序列的用户将无法找到其他具有不同但规范等效的代码点表示的视觉上无法区分的字形。
see: http://en.wikipedia.org/wiki/Unicode_equivalence
请参阅:http: //en.wikipedia.org/wiki/Unicode_equivalence
If you are trying to compare 2 unicode strings in a case insensitive way and want it to work EVERYWHERE, you have an impossible problem.
如果你想在不区分大小写的方式来比较2个unicode字符串,并希望它的工作无处不在,你有一个不可能的问题。
The classic example is the Turkish i, which when uppercased becomes ? (notice the dot)
经典的例子是土耳其语 i,当大写时变成 ? (注意点)
By default, the .Net framework usually uses the CurrentCulturefor string related functions, with a very important exception of .Equalsthat uses an ordinal (byte by byte) compare.
默认情况下,.Net 框架通常将CurrentCulture用于与字符串相关的函数,一个非常重要的例外.Equals是使用序数(逐字节)比较。
This leads, by design, to the various string functions behaving differently depending on the computer's culture.
根据设计,这会导致各种字符串函数的行为因计算机文化而异。
Nonetheless, sometimes we want a "general purpose", case insensitive, comparison.
尽管如此,有时我们想要一个“通用”的、不区分大小写的比较。
For example, you may want your string comparison to behave the same way, no matter what computer your application is installed on.
例如,您可能希望字符串比较的行为方式相同,无论您的应用程序安装在哪台计算机上。
To achieve this we have 3 options:
为了实现这一点,我们有 3 个选择:
- Set the culture explicitly and perform a case insensitive compare using unicode equivalence rules.
- Set the culture to the Invariant Culture and perform case insensitive compare using unicode equivalence rules.
- Use OrdinalIgnoreCasewhich will uppercase the string using the InvariantCulture and then perform a byte by byte comparison.
- 显式设置区域性并使用 unicode 等效规则执行不区分大小写的比较。
- 将文化设置为不变文化并使用 unicode 等效规则执行不区分大小写的比较。
- 使用OrdinalIgnoreCase,它将使用InvariantCulture将字符串大写,然后执行逐字节比较。
Unicode equivalence rules are complicated, which means using method 1) or 2) is more expensive than OrdinalIgnoreCase. The fact that OrdinalIgnoreCasedoes not perform any special unicode normalization, means that some strings that render in the same way on a computer screen, will notbe considered identical. For example: "\u0061\u030a"and "\u00e5"both render ?. However in a ordinal compare will be considered different.
Unicode 等价规则很复杂,这意味着使用方法 1) 或 2) 比使用OrdinalIgnoreCase. OrdinalIgnoreCase不执行任何特殊 unicode 规范化的事实意味着在计算机屏幕上以相同方式呈现的某些字符串将不被视为相同。例如:"\u0061\u030a"并且"\u00e5"都渲染?。但是在顺序比较中将被认为是不同的。
Which you choose heavily depends on the application you are building.
您选择哪个在很大程度上取决于您正在构建的应用程序。
- If I was writing a line-of-business app which was only used by Turkish users, I would be sure to use method 1.
- If I just needed a simple "fake" case insensitive compare, for say a column name in a db, which is usually English I would probably use method 3.
- 如果我正在编写一个仅供土耳其用户使用的业务线应用程序,我一定会使用方法 1。
- 如果我只需要一个简单的“假”不区分大小写的比较,例如数据库中的列名,通常是英语,我可能会使用方法 3。
Microsoft has their set of recommendationswith explicit guidelines. However, it is really important to understand the notion of unicode equivalence prior to approaching these problems.
Microsoft有一套带有明确指导方针的建议。然而,在解决这些问题之前理解 unicode 等价的概念非常重要。
Also, please keep in mind that OrdinalIgnoreCase is a very special kindof beast, that is picking and choosing a bit of an ordinal compare with some mixed in lexicographic aspects. This can be confusing.
另外,请记住,OrdinalIgnoreCase 是一种非常特殊的野兽,它正在挑选一些序数与一些在词典方面混合的比较。这可能会令人困惑。
回答by Bullines
I guess it depends on your situation. Since ordinal comparisons are actually looking at the characters' numeric Unicode values, they won't be the best choice when you're sorting alphabetically. For string comparisons, though, ordinal would be a tad faster.
我想这取决于你的情况。由于序数比较实际上是查看字符的数字 Unicode 值,因此当您按字母顺序排序时,它们不是最佳选择。但是,对于字符串比较,序数会快一点。
回答by Joel Coehoorn
It depends on what you want, though I'd shy away from invariantculture unless you're verysure you'll never want to localize the code for other languages. Use CurrentCulture instead.
这取决于你想要什么,尽管我会回避不变文化,除非你非常确定你永远不想为其他语言本地化代码。请改用 CurrentCulture。
Also, OrdinalIgnoreCase should respect numbers, which may or may not be what you want.
此外, OrdinalIgnoreCase 应该尊重数字,这可能是也可能不是您想要的。
回答by TheMoot
The very simple answer is, unless you are using Turkish, you don't need to use InvariantCulture.
非常简单的答案是,除非您使用土耳其语,否则您不需要使用 InvariantCulture。
See the following link:
请参阅以下链接:
In C# what is the difference between ToUpper() and ToUpperInvariant()?

