使用InvariantCultureIgnoreCase代替ToUpper的为不区分大小写字符串比较 [英] Using InvariantCultureIgnoreCase instead of ToUpper for case-insensitive string comparisons
问题描述
在<一个href="http://www.inq.me/post/ASPNet-MVC-Extension-method-to-create-a-Security-Aware-HtmlActionLink.aspx">this页面,一个评论者写道:的
On this page, a commenter writes:
千万不要使用 .ToUpper
来确保比较字符串不区分大小写。
Do NOT ever use .ToUpper
to insure comparing strings is case-insensitive.
type.Name.ToUpper() == (controllerName.ToUpper() + "Controller".ToUpper()))
做到这一点:
Do this:
type.Name.Equals(controllerName + "Controller",
StringComparison.InvariantCultureIgnoreCase)
为什么是这样preferred?的
推荐答案
下面是详细答案..的土耳其的测试(阅读部分的 3 的)
Here is the answer in details .. The Turkey Test (read section 3)
正如很多很多的讨论 人,在我土耳其的行为方式 比大多数语言不同。 按照统一code标准,我们的 小写的i变为I(U + 0130 拉丁大写字母我有了点 以上),当它移动到大写。 同样的,我们的大写的我变成 我(U + 0131小写拉丁字母 带点我),当它移动到 小写的。
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 .NET中使用字符串的新建议2.0 的)
And according to Microsoft you should not even be using the Invariant... but the Ordinal... (New Recommendations for Using Strings in Microsoft .NET 2.0)
这篇关于使用InvariantCultureIgnoreCase代替ToUpper的为不区分大小写字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!