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

发布于 2024-12-16 19:35:59 字数 61 浏览 2 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼藏柔 2024-12-23 19:35:59

如果将文化感知字符串与 == 进行比较,例如“Strasse”与“Straße”,则返回 false。

如果您需要对 UI 内容进行文化感知比较(Listview 的排序),您可以使用 String.Compare 和相关的 CultureInfo。

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

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.

CultureInfo ci = new CultureInfo("de-DE");
String.Compare("Strasse", "Straße", true, ci) // Returns zero
哀由 2024-12-23 19:35:59

== 不区分区域性 - 这是一个简单的序数比较。因此,在文化上相等(甚至在其他规范化形式方面相等)的两个字符串可能通过 == 可能相等。它基本上将每个字符串视为一个 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 a char array.

神回复 2024-12-23 19:35:59

重载的 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文