自定义大小写映射和排序规则

序列项的大小写映射、字母顺序排序以及约定在各区域性之间会有所不同。 您应该了解这些变化,并认识到这些变化可导致字符串操作的结果因区域性不同而异。

土耳其字母表的独特大小写映射规则说明了大小写映射在不同语言之间存在着差别,甚至是在它们使用的字母大多数都相同时也如此。 在大多数拉丁语字母表中,字符“i”(U+0069) 是字符的小写“i”(U+0049)。 但是,在土耳其语字母表两个版本的大写和小写“i”:一种带点和一个不带点。 在土耳其语中,字符“i”(U+0049) 被视为字符“ı”的大写版本 (U+0131),而“i”(U+0130) 被视为字符大写的“i”(U+0069)。 因此,字符的一个不区分大小写的字符串比较“i”(U+0069) 和“i”(U+0049) 对于大多数区域性成功为土耳其语的区域性失败 (土耳其),即 tr TR。

备注

阿塞拜疆区域性 (阿塞拜疆,拉丁语),即 az-Latn-AZ,也可以使用此大小写映射规则。

下面的代码示例演示了在对字符串“FILE”和“file”执行不区分大小写的 String.Compare 操作时,其结果如何随区域性而异。 则比较操作返回 true,如果 Thread.CurrentThread.CurrentCulture 属性设置为英语区域性 (美国),即 en-us。 则比较操作返回 false,如果当前区域性设置为土耳其语 (土耳其),即 tr TR。

Imports System.Globalization
Imports System.Threading

Public Class Example
   Public Shared Sub Main()
      ' Set the CurrentCulture property to English in the U.S.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      Console.WriteLine("Culture = {0}", _
                        Thread.CurrentThread.CurrentCulture.DisplayName)
      Console.WriteLine("(file == FILE) = {0}", String.Compare("file", _
                        "FILE", True) = 0)
      Console.WriteLine()

      ' Set the CurrentCulture property to Turkish in Turkey.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("tr-TR")
      Console.WriteLine("Culture = {0}", _
                        Thread.CurrentThread.CurrentCulture.DisplayName)
      Console.WriteLine("(file == FILE) = {0}", String.Compare("file", _
                        "FILE", True) = 0)
   End Sub
End Class
' The example displays the following output:
'      Culture = English (United States)
'      (file == FILE) = True
'
'      Culture = Turkish (Turkey)
'      (file == FILE) = False
using System;
using System.Globalization;
using System.Threading;

public class Example
{
    public static void Main()
    {
       // Set the CurrentCulture property to English in the U.S.
       Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
       Console.WriteLine("Culture = {0}",   
                         Thread.CurrentThread.CurrentCulture.DisplayName);
       Console.WriteLine("(file == FILE) = {0}\n", (string.Compare("file", 
                         "FILE", true) == 0));

       // Set the CurrentCulture property to Turkish in Turkey.
       Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
       Console.WriteLine("Culture = {0}",
                         Thread.CurrentThread.CurrentCulture.DisplayName);
       Console.WriteLine("(file == FILE) = {0}", (string.Compare("file", 
                         "FILE", true) == 0));
    }
}
// The example displays the following output:
//      Culture = English (United States)
//      (file == FILE) = True
//
//      Culture = Turkish (Turkey)
//      (file == FILE) = False

其他自定义大小写映射和排序规则

除了土耳其语和阿塞拜疆字母表的独特大小写映射之外,还有其他一些自定义大小写映射和排序规则应注意,在考虑字符串操作时。 在 ASCII 范围(Unicode 0000 到 Unicode 007F)中,有九个区域性的字母表包含由两个字母组成的字母对。对于这些字母对,不区分大小写的比较(例如,使用 String.Compare 进行的比较)在大小写混用时不会得到相等的结果。 这些区域性包括:

  • 克罗地亚语 (克罗地亚),hr HR

  • 捷克语 (捷克),cs-cz

  • 斯洛文尼亚语 (斯洛文尼亚),sk SK

  • 丹麦语 (丹麦),da DK

  • 挪威语 (博克马尔语,挪威),nb-no

  • 挪威语 (尼诺斯克,挪威),nn-no

  • 匈牙利语 (匈牙利),hu-hu

  • 越南语 (越南),vi VN

  • 西班牙语 (西班牙,传统排序),es-ES_tradnl

例如,在丹麦语中,对两个字母组成的字母对“aA”和“AA”进行不区分大小写的比较时,它们被认为是不相等的。 在越南语字母表中,对两个字母组成的字母对“nG”和“NG”进行不区分大小写的比较时,它们被认为是不相等的。 虽然您应该知道存在这些规则,但在实践中,很少会遇到在对这些字母对进行区分区域性的比较时出现问题的情况,因为这些字母对很少出现在固定字符串或标识符中。

ASCII 范围内六个区域性的字母表的大小写规则是标准的,但排序规则各不相同。 这些区域性包括:

  • 爱沙尼亚语 (爱沙尼亚),et-ee

  • 芬兰语 (芬兰),fi FI

  • 匈牙利语 (匈牙利,传统排序顺序),hu-HU_technl

  • 立陶宛语 (立陶宛),Lt LT

  • 瑞典语 (芬兰),sv FI

  • 瑞典语 (瑞典),sv SE

例如,在瑞典语字母表中,将字母“w”视为字母“v”来进行排序。 在应用程序代码中,排序操作的使用频率低于相等性比较,因此排序操作产生问题的可能性较小。

其他 35 个区域性具有 ASCII 范围外的自定义大小写映射和排序规则。 这些规则通常局限于特定区域性所使用的字母表。 因此,它们造成问题的可能性非常小。

有关适用于特定区域性的自定义大小写映射和排序规则的详细信息,请参见位于 Unicode home page(Unicode 主页)的“Unicode Standard”(Unicode 标准)。

请参见

概念

不区分区域性的字符串操作

其他资源

执行不区分区域性的字符串操作