简体   繁体   English

我应该使用'=='进行.NET本地化字符串比较吗?

[英]Should I use '==' for .NET localized string comparisons?

What are the reasons not to use "==" to compare localized strings in .NET? 不使用“==”来比较.NET中的本地化字符串的原因是什么? How would the comparison execute in regards to the CultureInfo if I do use it? 如果我使用它,比较如何执行CultureInfo?

If you compare culture-aware strings with ==, for example "Strasse" with "Straße", it returns false. 如果将文化感知字符串与==进行比较,例如将“Strasse”与“Straße”进行比较,则返回false。

If you need culture-aware comparings for UI stuff (Sorting of Listview), you use String.Compare with the related CultureInfo. 如果您需要针对UI内容的文化感知比较(Listview的排序),则将String.Compare与相关的CultureInfo一起使用。

CultureInfo ci = new CultureInfo("de-DE");
String.Compare("Strasse", "Straße", true, ci) // Returns zero

== 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 a char array. 它基本上将每个字符串视为char数组。

The overloaded String.operator == will perform an culture-unaware ordinal comparison – it compares the strings byte-by-byte using a heavily optimized unrolled loop . 重载的String.operator ==将执行不String.operator ==文化的序数比较 - 它使用大量优化的展开循环逐字节地比较字符串。
It calls the same internal function as String.Equals(a, b, StringComparison.Ordinal) 它调用与String.Equals(a, b, StringComparison.Ordinal)相同的内部函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM