gpt4 book ai didi

c# - 使用 InvariantCultureIgnoreCase 而不是 ToUpper 进行不区分大小写的字符串比较

转载 作者:可可西里 更新时间:2023-11-01 07:53:21 26 4
gpt4 key购买 nike

this page ,一位评论者写道:

永远不要使用 .ToUpper 来确保比较字符串时不区分大小写。

取而代之的是:

type.Name.ToUpper() == (controllerName.ToUpper() + "Controller".ToUpper())) 

这样做:

type.Name.Equals(controllerName + "Controller", 
StringComparison.InvariantCultureIgnoreCase)

为什么首选这种方式?

最佳答案

这里是详细的答案.. The Turkey Test (阅读第 3 部分)

As discussed by lots and lots of people, the "I" in Turkish behaves differently than in most languages. Per the Unicode standard, our lowercase "i" becomes "İ" (U+0130 "Latin Capital Letter I With Dot Above") when it moves to uppercase. Similarly, our uppercase "I" becomes "ı" (U+0131 "Latin Small Letter Dotless I") when it moves to lowercase.

Fix: Again, use an ordinal (raw byte) comparer, or invariant culture for comparisons unless you absolutely need culturally based linguistic comparisons (which give you uppercase I's with dots in Turkey)

根据 Microsoft 的说法,您甚至不应该使用不变量...而应该使用序数...( New Recommendations for Using Strings in Microsoft .NET 2.0 )

关于c# - 使用 InvariantCultureIgnoreCase 而不是 ToUpper 进行不区分大小写的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2256453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com