固有カルチャのデータの比較と並べ替え

更新 : 2007 年 11 月

項目を並べ替えるときのアルファベット順と規則は、カルチャによって異なります。たとえば、並べ替え順序で大文字小文字が区別される場合と区別されない場合があります。また、並べ替え順序が文字の発音に基づいている場合と基づいていない場合があります。東アジア圏の言語では、表意文字の画数と部首によって並べ替え順序が設定されています。並べ替えは、言語とカルチャでのアルファベットの基本的な順序によって異なります。たとえば、スウェーデン語の文字 "Æ" は、アルファベットでは "Z" の後に位置します。ドイツ語にも同じ文字がありますが、ドイツ語アルファベットでは "ae" として処理され、"A" の後に位置します。国際対応アプリケーションでは、カルチャ固有および言語固有の並べ替え規則をサポートするため、カルチャ別にデータの比較と並べ替えを実行できる必要があります。

メモ   カルチャに依存した動作が意図するものと異なる場合があります。カルチャを認識しない操作を行う場合と、その操作の実行方法の詳細については、「カルチャを認識しない文字列操作」を参照してください。

文字列の比較

CompareInfo クラスには、カルチャに依存した文字列比較を実行するためのメソッドのセットが含まれています。CultureInfo クラスには、このクラスのインスタンスである CompareInfo プロパティがあります。このプロパティによって、固有カルチャでの文字列の比較方法と並べ替え方法が定義されます。Compare() メソッドは、CompareInfo プロパティの情報を使用して文字列を比較します。String.Compare メソッドは、string1 が string2 未満の場合に負の整数値を返し、string1 と string2 が等しい場合に 0 を返し、string1 が string2 よりも大きい場合に正の整数値を返します。

String.Compare メソッドによる 2 つの文字列の評価方法が、比較に使用されるカルチャによって異なることを次のコード例に示します。最初に、CurrentCulture をデンマーク語 (デンマーク) に設定し、文字列 "Apple" と ""Æble" を比較します。デンマーク語では、文字 "Æ" は 1 文字として扱われ、アルファベット順では "Z" の後に位置付けられます。したがって、デンマークのカルチャでは、文字列 "Æble" は "Apple" よりも大きい値です。次に、CurrentCulture を英語 (U.S.) に設定し、文字列 "Apple" と "Æble" を比較します。文字列 "Æble" は "Apple" よりも小さい値として判断されます。英語では、文字 "Æ" は特殊記号として扱われ、アルファベット順では "A" の前に位置付けられます。

Imports System
Imports System.Globalization
Imports System.Threading
Imports Microsoft.VisualBasic

Public Class TestClass
   Public Shared Sub Main()
      Dim str1 As String = "Apple"
      Dim str2 As String = "Æble"
      
      ' Sets the CurrentCulture to Danish in Denmark.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("da-DK")
      Dim result1 As Integer = [String].Compare(str1, str2)
      Console.WriteLine(ControlChars.Newline + "When the CurrentCulture _
         is ""da-DK""," + ControlChars.Newline + " the result of _
         comparing_{0} with {1} is: {2}", str1, str2, result1)
      
      ' Sets the CurrentCulture to English in the U.S.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      Dim result2 As Integer = [String].Compare(str1, str2)
      Console.WriteLine(ControlChars.Newline + "When the _
         CurrentCulture is""en-US""," + ControlChars.Newline + " _
         the result of comparing {0} with {1} is: {2}", str1, _
         str2,result2)
   End Sub
End Class
using System;
using System.Globalization;
using System.Threading;

public class CompareStringSample
{
   public static void Main()
   {
      string str1 = "Apple";
      string str2 = "Æble"; 

      // Sets the CurrentCulture to Danish in Denmark.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
      // Compares the two strings.
      int result1 = String.Compare(str1, str2);
      Console.WriteLine("\nWhen the CurrentCulture is \"da-DK\",\nthe 
            result of comparing {0} with {1} is: {2}",str1, str2, 
            result1);

      // Sets the CurrentCulture to English in the U.S.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      // Compares the two strings.
      int result2 = String.Compare(str1, str2);
      Console.WriteLine("\nWhen the CurrentCulture is \"en-US\",\nthe 
            result of comparing {0} with {1} is: {2}",str1, str2, 
            result2);
   }
}

このコードを実行すると、出力は次のようになります。

When the CurrentCulture is "da-DK", 
the result of comparing Apple with Æble is: -1

When the CurrentCulture is "en-US", 
the result of comparing Apple with Æble is: 1

文字列比較の詳細については、「文字列の比較」を参照してください。

代替並べ替え順序の使用

一部のカルチャでは、複数の並べ替え順序がサポートされています。たとえば、カルチャ "zh-CN" (中国の中国語) では、発音による並べ替え (既定) と画数による並べ替えがサポートされています。アプリケーションが "zh-CN" などのカルチャ名を使用して CultureInfo オブジェクトを作成すると、既定の並べ替え順序が使用されます。代替並べ替え順序を指定するには、アプリケーションが代替並べ替え順序の LCID を使用して CultureInfo オブジェクトを作成する必要があります。次に、アプリケーションは文字列の比較で使用する CompareInfo オブジェクトを CompareInfo から取得します。また、GetCompareInfo() を使用し、代替並べ替え順序としてロケール識別子 (LCID) を指定して、CompareInfo オブジェクトを直接作成することもできます。

代替並べ替え順序をサポートするカルチャと、各カルチャの既定並べ替え順序と代替並べ替え順序の LCID を次の表に示します。

カルチャ名

カルチャ

既定の並べ替え名と LCID

代替並べ替え名と LCID

es-ES

スペイン語 (スペイン)

International: 0x00000C0A

Traditional: 0x0000040A

zh-TW

中国語 (台湾)

Stroke Count: 0x00000404

Bopomofo: 0x00030404

zh-CN

中国語 (中国)

Pronunciation: 0x00000804

Stroke Count: 0x00020804

zh-HK

中国語 (香港特別行政区)

Stroke Count: 0x00000c04

Stroke Count: 0x00020c04

zh-SG

中国語 (シンガポール)

Pronunciation: 0x00001004

Stroke Count: 0x00021004

zh-MO

中国語 (マカオ)

Pronunciation: 0x00001404

Stroke Count: 0x00021404

ja-JP

日本語 (日本)

Default: 0x00000411

Unicode: 0x00010411

ko-KR

韓国語 (韓国)

Default: 0x00000412

Korean Xwansung - Unicode: 0x00010412

de-DE

ドイツ語 (ドイツ)

Dictionary: 0x00000407

Phone Book Sort DIN: 0x00010407

hu-HU

ハンガリー語 (ハンガリー)

Default: 0x0000040e

Technical Sort: 0x0001040e

ka-GE

グルジア語 (グルジア共和国)

Traditional: 0x00000437

Modern Sort: 0x00010437

文字列の検索

アプリケーションでは、オーバーロードされた IndexOf() メソッドを使用して、指定された文字列内の文字または部分文字列のインデックスを取得できます。このインデックスはゼロから始まります。指定された文字列内で文字または部分文字列が見つからない場合、このメソッドは負の整数値を取得します。CompareInfo.IndexOf を使用して指定文字列を検索する場合、アプリケーションでは、CompareOptions パラメータを受け取るメソッド オーバーロードと、このパラメータを受け取らないメソッド オーバーロードでは実行される比較が異なる点を考慮に入れる必要があります。文字型を検索し、CompareOptions パラメータを受け取らないメソッド オーバーロードは、カルチャに依存した検索を実行します。つまり、Unicode 値が "Æ" (\u00C6) のような合字 (複数文字で構成されている 1 文字) を表す場合、カルチャによっては、"AE" (\u0041\u0045) のように 2 つの文字が並んでいるのと同じと見なされることがあります。ある文字型と別の文字型をそれらの Unicode 値が一致する場合のみ等価と見なす序数 (カルチャを認識しない) 検索を実行するには、アプリケーションでは CompareOptions パラメータを受け取って序数値に設定する CompareInfo.IndexOf オーバーロードのいずれかを使用する必要があります。

また、文字を検索する IndexOf() メソッドのオーバーロードを使用して、序数 (カルチャを認識しない) 検索を実行することもできます。このメソッドのオーバーロードのうち文字列を検索するものはカルチャを認識する検索を実行する点に注意してください。

使用されるカルチャによって IndexOf メソッドが取得する結果が異なることを次のコード例に示します。このコードでは、デンマーク語 (デンマーク) カルチャ "da-DK" の CultureInfo オブジェクトを作成します。次に、CompareInfo.IndexOf メソッドのオーバーロードを使用して、文字列 "Æble" と "aeble" で文字 "Æ" を検索します。カルチャ "da-DK" では、Ordinal に設定された CompareOptions パラメータを受け取る CompareInfo.IndexOf メソッドと、このパラメータを受け取らない同一メソッドが同じ結果を取得します。文字 "Æ" だけが、この Unicode コード値 \u00E6 と等価であると見なされます。

Imports System
Imports System.Globalization
Imports System.Threading
Imports Microsoft.VisualBasic

Public Class CompareClass
   Public Shared Sub Main()
      Dim str1 As String = "Æble"
      Dim str2 As String = "aeble"
      Dim find As Char = "Æ"
      
      ' Creates a CultureInfo for Danish in Denmark.
      Dim ci As New CultureInfo("da-DK")
      
      Dim result1 As Integer = ci.CompareInfo.IndexOf(str1, find)
      Dim result2 As Integer = ci.CompareInfo.IndexOf(str2, find)
      Dim result3 As Integer = ci.CompareInfo.IndexOf(str1, find, _ 
         CompareOptions.Ordinal)
      Dim result4 As Integer = ci.CompareInfo.IndexOf(str2, find, _
         CompareOptions.Ordinal)      
      
      Console.WriteLine(ControlChars.Newline + "CultureInfo is set to _
         {0}", ci.DisplayName)
      Console.WriteLine(ControlChars.Newline + "Using _
         CompareInfo.IndexOf(string, char) method" + _
         ControlChars.Newline + " the result of searching for {0} in the _
         string {1} is: {2}", find, str1, result1)
      Console.WriteLine(ControlChars.Newline + "Using _
         CompareInfo.IndexOf(string, char) method" + _
         ControlChars.Newline + " the result of searching for {0} in the _
         string {1} is: {2}", find, str2, result2)
      Console.WriteLine(ControlChars.Newline + "Using _
         CompareInfo.IndexOf(string, char, CompareOptions) method" + _
         ControlChars.Newline + " the result of searching for {0} in the _
         string {1} is: {2}", find, str1, result3)
      Console.WriteLine(ControlChars.Newline + "Using _
         CompareInfo.IndexOf(string, char, CompareOptions) method" + _
         ControlChars.Newline + " the result of searching for {0} in the _
         string {1} is: {2}", find, str2, result4)
   End Sub
End Class
using System;
using System.Globalization;
using System.Threading;

public class CompareClass
{

   public static void Main()
   {
      string str1 = "Æble";
      string str2 = "aeble"; 
      char find = 'Æ';

      // Creates a CultureInfo for Danish in Denmark.
      CultureInfo ci= new CultureInfo("da-DK");

      int result1 = ci.CompareInfo.IndexOf(str1, find);
      int result2 = ci.CompareInfo.IndexOf(str2, find);
      int result3 = ci.CompareInfo.IndexOf(str1, find,   
         CompareOptions.Ordinal);
      int result4 = ci.CompareInfo.IndexOf(str2, find, 
         CompareOptions.Ordinal);

      Console.WriteLine("\nCultureInfo is set to {0} ", ci.DisplayName);
      Console.WriteLine("\nUsing CompareInfo.IndexOf(string, char) 
         method\nthe result of searching for {0} in the string {1} is: 
         {2}", find, str1, result1);
      Console.WriteLine("\nUsing CompareInfo.IndexOf(string, char) 
         method\nthe result of searching for {0} in the string {1} is: 
         {2}", find, str2, result2);
      Console.WriteLine("\nUsing CompareInfo.IndexOf(string, char, 
         CompareOptions) method\nthe result of searching for {0} in the 
         string {1} is: {2}", find, str1, result3);
      Console.WriteLine("\nUsing CompareInfo.IndexOf(string, char, 
         CompareOptions) method\nthe result of searching for {0} in the 
         string {1} is: {2}", find, str2, result4);
   }
}

このコードを実行すると、次の出力が生成されます。

CultureInfo is set to Danish (Denmark) 

Using CompareInfo.IndexOf(string, char) method
the result of searching for Æ in the string Æble is: 0

Using CompareInfo.IndexOf(string, char) method
the result of searching for Æ in the string aeble is: -1

Using CompareInfo.IndexOf(string, char, CompareOptions) method
the result of searching for Æ in the string Æble is: 0

Using CompareInfo.IndexOf(string, char, CompareOptions) method
the result of searching for Æ in the string aeble is: -1

アプリケーションが CultureInfo ci = new CultureInfo ("da-DK") を CultureInfo ci = new CultureInfo ("en-US") に置き換えると、Ordinal に設定された CompareOptions パラメータを受け取る IndexOf() メソッドと、このパラメータを受け取らない同一メソッドは異なる結果を取得します。IndexOf メソッドによって実行されるカルチャを認識する比較では、文字 "Æ" がこの文字を構成する 2 文字 "ae" と等価であると評価されます。IndexOf メソッドによって実行される序数 (カルチャを認識しない) 比較では、文字 "Æ" と "ae" の Unicode コード値が一致しないため、これらの値が等価であるとは評価されません。

このコードを再コンパイルして英語 (U.S.) カルチャ "en-US" に対して実行すると、次のような出力が生成されます。

The CurrentCulture property is set to English (United States) 

Using CompareInfo.IndexOf(string, char) method
the result of searching for Æ in the string Æble is: 0

Using CompareInfo.IndexOf(string, char) method
the result of searching for Æ in the string aeble is: 0

Using CompareInfo.IndexOf(string, char, CompareOptions) method
the result of searching for Æ in the string Æble is: 0

Using CompareInfo.IndexOf(string, char, CompareOptions) method
the result of searching for Æ in the string aeble is: -1

文字列の並べ替え

Array クラスのオーバーロードされた Sort() メソッドを使用すると、アプリケーションで CurrentCulture プロパティに基づいて配列を並べ替えることができます。3 つの文字列からなる配列を作成する例を次に示します。まず、CurrentCulture プロパティを "en-US" で示される英語 (U.S.) に設定し、Sort() メソッドを呼び出しています。これよって、英語 (U.S.) カルチャの並べ替え規則に基づく並べ替え順序が適用されます。次に CurrentCulture プロパティを "da-DK" で示されるデンマーク語 (デンマーク) に設定し、再度 Array.Sort メソッドを呼び出します。結果の並べ替え順序が、"en-US" の並べ替え順序と異なる点に注意してください。これは、"da-DK" カルチャの並べ替え規則が使用されるためです。

Imports System
Imports System.Threading
Imports System.IO
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class TextToFile   
   Public Shared Sub Main()
      Dim str1 As [String] = "Apple"
      Dim str2 As [String] = "Æble"
      Dim str3 As [String] = "Zebra"
      
      ' Creates and initializes a new Array to store 
      ' these date/time objects.
      Dim stringArray As Array = Array.CreateInstance(GetType([String]), _
         3)
      stringArray.SetValue(str1, 0)
      stringArray.SetValue(str2, 1)
      stringArray.SetValue(str3, 2)
      
      ' Displays the values of the Array.
      Console.WriteLine(ControlChars.Newline + "The Array initially _
         contains the following strings:")
      PrintIndexAndValues(stringArray)
      
      ' Sets the CurrentCulture to "en-US".
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      ' Sorts the values of the Array.
      Array.Sort(stringArray)
      
      ' Displays the values of the Array.
      Console.WriteLine(ControlChars.Newline + "After sorting for the _
         culture ""en-US"":")
      PrintIndexAndValues(stringArray)
      
      ' Sets the CurrentCulture to "da-DK".
      Thread.CurrentThread.CurrentCulture = New CultureInfo("da-DK")
      ' Sort the values of the Array.
      Array.Sort(stringArray)
      
      ' Displays the values of the Array.
      Console.WriteLine(ControlChars.Newline + "After sorting for the _
         culture ""da-DK"":")
      PrintIndexAndValues(stringArray)
   End Sub

   Public Shared Sub PrintIndexAndValues(myArray As Array)
      Dim i As Integer
      For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
         Console.WriteLine(ControlChars.Tab + "[{0}]:" + _
            ControlChars.Tab + "{1}", i, myArray.GetValue(i))
      Next i
   End Sub 
End Class
using System;
using System.Threading;
using System.Globalization;

public class ArraySort 
{
   public static void Main(String[] args) 
   {
      String str1 = "Apple";
      String str2 = "Æble";
      String str3 = "Zebra";

      // Creates and initializes a new Array to store the strings.
      Array stringArray = Array.CreateInstance( typeof(String), 3 );
      stringArray.SetValue(str1, 0 );
      stringArray.SetValue(str2, 1 );
      stringArray.SetValue(str3, 2 );

      // Displays the values of the Array.
      Console.WriteLine( "\nThe Array initially contains the following 
            strings:" );
      PrintIndexAndValues(stringArray);

      // Sets the CurrentCulture to "en-US".
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      // Sort the values of the Array.
      Array.Sort(stringArray);

      // Displays the values of the Array.
      Console.WriteLine( "\nAfter sorting for the culture \"en-US\":" );
      PrintIndexAndValues(stringArray); 

      // Sets the CurrentCulture to "da-DK".
      Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
      // Sort the values of the Array.
      Array.Sort(stringArray);

      // Displays the values of the Array.
      Console.WriteLine( "\nAfter sorting for the culture \"da-DK\":" );
      PrintIndexAndValues(stringArray); 
   }
   public static void PrintIndexAndValues(Array myArray)  
   {
      for ( int i = myArray.GetLowerBound(0); i <= 
            myArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}

このコードを実行すると、次の出力が生成されます。

The Array initially contains the following strings:
   [0]:   Apple
   [1]:   Æble
   [2]:   Zebra

After sorting for the culture "en-US":
   [0]:   Æble
   [1]:   Apple
   [2]:   Zebra

After sorting for the culture "da-DK":
   [0]:   Apple
   [1]:   Zebra
   [2]:   Æble

並べ替えキーの使用

カルチャ認識並べ替えをサポートするには、並べ替えキーを使用します。Unicode 規格に基づいて、文字列の各文字には、アルファベット順のウェイト、大文字小文字のウェイト、発音のウェイトなどのさまざまな並べ替えウェイトが割り当てられています。並べ替えキーは、特定の文字列に対するこれらのウェイトを格納するリポジトリとして機能します。たとえば、並べ替えキーにはアルファベット順ウェイトの文字列、大文字小文字のウェイトの文字列などが特定の順序で格納されています。並べ替えキーの概念の詳細については、www.unicode.org の「Unicode Standard」を参照してください。

.NET Framework では、SortKey クラスによって、並べ替えキーから文字列、または文字列から並べ替えキーへの割り当てが行われます。アプリケーションで指定する文字列の並べ替えキーを作成するには、GetSortKey() メソッドを使用します。このメソッドにより作成される指定文字列の並べ替えキーは、CurrentCultureCompareOptions の指定値によって異なるバイト シーケンスです。たとえば、アプリケーションが並べ替えキーを作成するときに値 IgnoreCase を指定すると、この並べ替えキーを使用した文字列比較操作では大文字小文字が区別されません。

アプリケーションは、作成した文字列の並べ替えキーを SortKey クラスのメソッドにパラメータとして渡すことができます。Compare メソッドを使用すると、並べ替えキーを比較できます。このメソッドは単純なバイトごとの比較を実行するため、Compare() を使用するよりはるかに高速です。並べ替え操作を頻繁に実行するアプリケーションでは、使用するすべての文字列に対して並べ替えキーを生成し格納することにより、パフォーマンスを高めることができます。並べ替え操作または比較操作が必要な場合には、アプリケーションで文字列ではなく並べ替えキーを使用できます。

CurrentCulture が "da-DK" に設定されている場合に 2 つの文字列の並べ替えキーを作成するコード例を次に示します。SortKey.Compare メソッドを使用して 2 つの文字列が比較され、比較結果が表示されます。このメソッドは、string1 が string2 未満の場合に負の整数値を返し、string1 と string2 が等しい場合に 0 を返し、string1 が string2 よりも大きい場合に正の整数値を返します。CurrentCulture プロパティが "en-US" カルチャに設定され、同じ文字列に対して並べ替えキーが作成されます。文字列の並べ替えキーが比較され、比較結果が表示されます。並べ替え結果が CurrentCulture の設定によって異なる点に注意してください。次のコード例の実行結果は、前の「文字列の比較」の例の文字列比較結果と同一ですが、SortKey.Compare メソッドは String.Compare メソッドより処理が高速です。

Imports System
Imports System.Threading
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class SortKeySample
   Public Shared Sub Main()
      Dim str1 As [String] = "Apple"
      Dim str2 As [String] = "Æble"
      
      ' Sets the CurrentCulture to "da-DK".
      Dim dk As New CultureInfo("da-DK")
      Thread.CurrentThread.CurrentCulture = dk
      
      ' Creates a culturally sensitive sort key for str1.
      Dim sc1 As SortKey = dk.CompareInfo.GetSortKey(str1)
      ' Create a culturally sensitive sort key for str2.
      Dim sc2 As SortKey = dk.CompareInfo.GetSortKey(str2)
      
      ' Compares the two sort keys and display the results.
      Dim result1 As Integer = SortKey.Compare(sc1, sc2)
      Console.WriteLine(ControlChars.Newline + "When the CurrentCulture _
         is ""da-DK""," + ControlChars.Newline + " the result of _
         comparing {0} with {1} is: {2}", str1, str2, result1)
      
      ' Sets the CurrentCulture to "en-US".
      Dim enus As New CultureInfo("en-US")
      Thread.CurrentThread.CurrentCulture = enus
      
      ' Creates a culturally sensitive sort key for str1.
      Dim sc3 As SortKey = enus.CompareInfo.GetSortKey(str1)
      ' Create a culturally sensitive sort key for str1.
      Dim sc4 As SortKey = enus.CompareInfo.GetSortKey(str2)
      
      ' Compares the two sort keys and display the results.
      Dim result2 As Integer = SortKey.Compare(sc3, sc4)
      Console.WriteLine(ControlChars.Newline + "When the CurrentCulture _
         is ""en-US""," + ControlChars.Newline + " the result of _
         comparing {0} with {1} is: {2}", str1, str2, result2)
   End Sub
End Class
using System;
using System.Threading;
using System.Globalization;

public class SortKeySample 
{
   public static void Main(String[] args) 
   {
      String str1 = "Apple";
      String str2 = "Æble";

      // Sets the CurrentCulture to "da-DK".
      CultureInfo dk = new CultureInfo("da-DK");
      Thread.CurrentThread.CurrentCulture = dk;

      // Creates a culturally sensitive sort key for str1.
      SortKey sc1 = dk.CompareInfo.GetSortKey(str1);
      // Create a culturally sensitive sort key for str2.
      SortKey sc2 = dk.CompareInfo.GetSortKey(str2);

      // Compares the two sort keys and display the results.
      int result1 = SortKey.Compare(sc1, sc2);
      Console.WriteLine("\nWhen the CurrentCulture is \"da-DK\",\nthe 
            result of comparing {0} with {1} is: {2}", str1, str2, 
            result1);

      // Sets the CurrentCulture to "en-US".
      CultureInfo enus = new CultureInfo("en-US");
      Thread.CurrentThread.CurrentCulture = enus ;

      // Creates a culturally sensitive sort key for str1.
      SortKey sc3 = enus.CompareInfo.GetSortKey(str1);
      // Create a culturally sensitive sort key for str1.
      SortKey sc4 = enus.CompareInfo.GetSortKey(str2);

      // Compares the two sort keys and display the results.
      int result2 = SortKey.Compare(sc3, sc4);
      Console.WriteLine("\nWhen the CurrentCulture is \"en-US\",\nthe 
            result of comparing {0} with {1} is: {2}", str1, str2, 
            result2);
   }
}

このコードを実行すると、次の出力が生成されます。

When the CurrentCulture is "da-DK", 
the result of comparing Apple with Æble is: -1

When the CurrentCulture is "en-US", 
the result of comparing Apple with Æble is: 1

正規化

アプリケーションでは、並べ替えを実行する前に文字列を大文字または小文字に正規化できます。文字列の並べ替えと大文字小文字の区別の規則は、言語によって異なります。たとえば、ローマ字を利用する言語間でも、構成規則と並べ替え規則は異なります。英語などのいくつかの言語では、並べ替え順序とコード ポイントの順序が一致します。たとえば、A [65] の後に B [66] が位置付けられます。

アプリケーションで正確な並べ替えと文字列比較を実行するためには、コード ポイントに依存しないことが必要です。また .NET Framework が特定の正規化形式を強制的に適用したり、保証したりすることはありません。開発者が各自の責任で、開発するアプリケーションで適切な正規化を実行します。

文字列の正規化の詳細については、「正規化と並べ替え」を参照してください。

参照

概念

カルチャを認識しない文字列操作

正規化と並べ替え

その他の技術情報

エンコーディングとローカリゼーション