StringComparison.Ordinal 与 InvariantCulture 相同用于测试相等性吗?

发布于 2024-10-11 21:20:53 字数 362 浏览 9 评论 0原文

从简短的摘要描述来看,字符串比较规则 StringComparison.OrdinalStringComparison.InvariantCulture 的区别在于它们的排序方式字符串。这就是全部吗?即,这是否意味着我们在进行相等比较时可以使用任一字符串比较规则?

string.Equals(a, b, StringComparison....)

额外加分的是:如果我们比较 OrdinalIgnoreCase 和 InvariantCultureIgnoreCase ,答案会有所不同吗?如何?

请提供支持性论据和/或参考文献。

From their brief summary descriptions, it sounds like the string comparison rules StringComparison.Ordinal and StringComparison.InvariantCulture are meant to differ in how they do sorting of strings. Is that all? i.e., does that mean we can use either string comparison rule when doing an equality comparison?

string.Equals(a, b, StringComparison....)

And for extra credit: does it make a difference to the answer if we compare OrdinalIgnoreCase and InvariantCultureIgnoreCase? How?

Please provide supporting argument and/or references.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

凶凌 2024-10-18 21:20:53

例如,它确实很重要 - 有一种称为字符扩展的东西

    var s1 = "Strasse";
    var s2 = "Straße";

    s1.Equals(s2, StringComparison.Ordinal);          // false
    s1.Equals(s2, StringComparison.InvariantCulture); // true

,使用 InvariantCultureß 字符会扩展为 ss

It does matter, for example - there is a thing called character expansion

    var s1 = "Strasse";
    var s2 = "Straße";

    s1.Equals(s2, StringComparison.Ordinal);          // false
    s1.Equals(s2, StringComparison.InvariantCulture); // true

With InvariantCulture the ß character gets expanded to ss.

明媚如初 2024-10-18 21:20:53

嗯,这当然很重要。当您使用“忽略大小写”相等比较时,您将调用 .NET 框架中相当大的代码块,这些代码了解大小写规则在当前文化中的工作方式。对于像我这样的前邮票收藏极客来说,其中的规则非常有趣,根据你的观察,有一些相当奇怪的规则。土耳其语 I 问题很出名,Unicode 开发者不得不为他们做出明确的例外。

顺便说一句,它实际上不是代码,而是查找表。其本身很有趣,因为它需要 MSFT 维护 C# 编译器的 /linkres 命令行选项。您不能在自己的项目中使用的编译选项。它只是为了让 mscorlib 能够找到 .nlp 文件,即区域性规则的转换表。与 mscorlib.dll 存储在 GAC 的同一子目录中,这是编译选项的作用。

但我离题了。按理说 StringComparison.OrdinalIgnoreCase< /a> 比 StringComparison.InvariantCultureIgnoreCase 快一点。因为“不变”指的是美国,MSFT 的故乡。很难测量,这个时间以纳秒为单位。 StringComparison.CurrentCultureIgnoreCase 会命中这些转换表。第一次使用时速度非常慢,以后使用时速度会更慢。

Well, it certainly matters. When you use an "ignore case" equality comparison then you're invoking a fairly massive chunk of code in the .NET framework that's aware of how casing rules work in the current culture. The rules of which are very interesting to a former-postage-stamp collector geek like me, there are some pretty odd-ball rules depending where you look. The Turkish I problem is famous, the Unicode dudes had to make an explicit exception for them.

It isn't actually code btw, it's lookup tables. Interesting in itself because it requires MSFT to maintain the /linkres command line option for the C# compiler. A compile option you cannot use in your own projects. It's solely there to get mscorlib to be able to find the .nlp files, the conversion tables for culture rules. Stored in the same subdirectory of the GAC as mscorlib.dll, the effect of the compile option.

But I digress. It stands to reason that StringComparison.OrdinalIgnoreCase is a wee bit quicker than StringComparison.InvariantCultureIgnoreCase. Just because 'invariant' means USA, home of MSFT. Hard to measure, this clocks in at nanoseconds. StringComparison.CurrentCultureIgnoreCase hits those translation tables. Dead slow when you first use it, just slower when you use them later.

听风吹 2024-10-18 21:20:53

对于额外学分问题

For the extra credit question

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