我应该使用 '=='用于 .NET 本地化字符串比较?
.NET 中不使用“==”比较本地化字符串的原因是什么?如果我使用 CultureInfo,将如何执行比较?
What are the reasons not to use "==" to compare localized strings in .NET? How would the comparison execute in regards to the CultureInfo if I do use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将文化感知字符串与 == 进行比较,例如“Strasse”与“Straße”,则返回 false。
如果您需要对 UI 内容进行文化感知比较(Listview 的排序),您可以使用 String.Compare 和相关的 CultureInfo。
If you compare culture-aware strings with ==, for example "Strasse" with "Straße", it returns false.
If you need culture-aware comparings for UI stuff (Sorting of Listview), you use String.Compare with the related CultureInfo.
==
不区分区域性 - 这是一个简单的序数比较。因此,在文化上相等(甚至在其他规范化形式方面相等)的两个字符串可能通过==
可能不相等。它基本上将每个字符串视为一个char
数组。==
is culture-insensitive - it's a simple ordinal comparison. So two strings which are culturally equal - or even equal in terms of other canonicalization forms - may not be equal via==
. It basically treats each string like achar
array.重载的
String.operator ==
将执行不区分区域性的序数比较 - 它使用 深度优化的展开循环。它调用与 String.Equals(a, b, StringComparison.Ordinal) 相同的内部函数
The overloaded
String.operator ==
will perform an culture-unaware ordinal comparison – it compares the strings byte-by-byte using a heavily optimized unrolled loop.It calls the same internal function as
String.Equals(a, b, StringComparison.Ordinal)