使用InvariantCultureIgnoreCase而不是ToUpper用于不区分大小写的字符串比较 [英] Using InvariantCultureIgnoreCase instead of ToUpper for case-insensitive string comparisons

查看:131
本文介绍了使用InvariantCultureIgnoreCase而不是ToUpper用于不区分大小写的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此页,评论者写道:

不要使用 .ToUpper 以确保比较字符串不区分大小写。

Do NOT ever use .ToUpper to insure comparing strings is case-insensitive.

而不是:

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

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

Why is this way preferred?

推荐答案

这是详细的答案。土耳其测试阅读 3

Here is the answer in details .. The Turkey Test (read section 3)


正如大量的
人所讨论的,土耳其语中的I与大多数语言中的
不同。
根据Unicode标准,当它移动到大写时,我们的
小写字母i变成İ(U + 0130
Latin Capital Letter I With Dot
Above)。
类似地,当它移动到
小写字母时,我们的大写字母I变成
ı(U + 0131Latin Small Letter
Dotless I)。

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.

修复:同样,使用序数(原始字节)
比较器或不变文化进行
比较,除非你绝对需要
基于文化的语言
比较(在土耳其给出大写
I的点)

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)

微软你不应该使用Invariant ...但是Ordinal ...( 在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆