当在c#中比较两个相等的字符串时,不变文化和序数比较之间有什么区别?
当前回答
不变量是一种语言上合适的比较类型。 序数是一种二进制类型的比较。(快) 参见http://www.siao2.com/2004/12/29/344136.aspx
其他回答
总是尝试在那些接受它作为重载的字符串方法中使用InvariantCulture。通过使用InvariantCulture,你是安全的。许多。net程序员可能不会使用这个功能,但如果您的软件将被不同的区域性使用,那么InvariantCulture是一个非常方便的特性。
不变量是一种语言上合适的比较类型。 序数是一种二进制类型的比较。(快) 参见http://www.siao2.com/2004/12/29/344136.aspx
Another handy difference (in English where accents are uncommon) is that an InvariantCulture comparison compares the entire strings by case-insensitive first, and then if necessary (and requested) distinguishes by case after first comparing only on the distinct letters. (You can also do a case-insensitive comparison, of course, which won't distinguish by case.) Corrected: Accented letters are considered to be another flavor of the same letters and the string is compared first ignoring accents and then accounting for them if the general letters all match (much as with differing case except not ultimately ignored in a case-insensitive compare). This groups accented versions of the otherwise same word near each other instead of completely separate at the first accent difference. This is the sort order you would typically find in a dictionary, with capitalized words appearing right next to their lowercase equivalents, and accented letters being near the corresponding unaccented letter.
序数比较严格地比较数字字符值,在第一个差值处停止。这种方法将大写字母与小写字母完全分开排序(重音字母可能也与这些字母分开),因此大写单词的排序将与它们的小写对等物完全不同。
InvariantCulture还认为大写字母大于小写字母,而Ordinal认为大写字母小于小写字母(这是在计算机还没有小写字母之前遗留下来的ASCII,大写字母先分配,因此比后面添加的小写字母的值更低)。
例如,通过顺序:“0”<“9”<”“< <“Z”“Ab”<”“<“Ab”< <“Z”“Ab”<”“<“Ab”<”“<“Ab”
InvariantCulture:“0”<“9”<“a”<“A”<“á”<“Á”<“ab”<“aB”<“Ab”<“áb”<“Áb”<“Z”<
不需要使用花哨的unicode字符示例来显示差异。这是我今天发现的一个令人惊讶的简单例子,它只包含ASCII字符。
根据ASCII表,0 (48,0x30)在顺序比较时小于_ (95,0x5F)。InvariantCulture会说相反的(PowerShell代码如下):
PS> [System.StringComparer]::Ordinal.Compare("_", "0")
47
PS> [System.StringComparer]::InvariantCulture.Compare("_", "0")
-1
InvariantCulture
使用“标准”字符顺序集(a,b,c,…)等等)。这与一些特定的语言环境形成对比,这些语言环境可能会对字符进行不同的排序('a-with-acute'可能在'a'之前或之后,这取决于语言环境,等等)。
序数
另一方面,它只查看代表字符的原始字节的值。
http://msdn.microsoft.com/en-us/library/e6883c06.aspx上有一个很好的示例,展示了各种stringcompare值的结果。在最后,它显示(节选):
StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
你可以看到,其中InvariantCulture产生(U+0069, U+0049, U+00131),序数产生(U+0049, U+0069, U+00131)。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本