Share via


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

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

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

文字列の比較

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

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

Imports System.Globalization
Imports System.Threading

Public Class TestClass
   Public Shared Sub Main()
      Dim str1 As String = "Apple"
      Dim str2 As String = "Æble"

      ' Set the current culture to Danish in Denmark.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("da-DK")
      Dim result1 As Integer = [String].Compare(str1, str2)
      Console.WriteLine("When the CurrentCulture is ""da-DK"",")
      Console.WriteLine("the result of comparing_{0} with {1} is: {2}", 
                        str1, str2, result1)

      ' Set the current culture to English in the U.S.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      Dim result2 As Integer = [String].Compare(str1, str2)
      Console.WriteLine("When the CurrentCulture is""en-US"",")
      Console.WriteLine("the result of comparing {0} with {1} is: {2}", 
                        str1, str2,result2)
   End Sub
End Class
' The example displays the following output:
'    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
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);
   }
}
// The example displays the following output:
//    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 オブジェクトを作成すると、既定の並べ替え順序が使用されます。 代替の並べ替え順序を指定するには、アプリケーションは代替の並べ替え順序の識別子を使用して CultureInfo オブジェクトを作成する必要があります。 次に、アプリケーションは文字列の比較で使用する CompareInfo オブジェクトを CompareInfo から取得します。 また、アプリケーションは、CompareInfo.GetCompareInfo メソッドを使用して CompareInfo オブジェクトを直接作成し、代替の並べ替え順序の識別子を指定することもできます。

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

カルチャ名

Culture

既定の並べ替え名と識別子

代替の並べ替え名と識別子

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

文字列の検索

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

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

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

Imports System.Globalization
Imports System.Threading

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("CultureInfo is set to {0}", 
                        ci.DisplayName)
      Console.WriteLine()
      Console.WriteLine("Using CompareInfo.IndexOf(string, char) method")
      Console.WriteLine("the result of searching for {0} in the string {1} is: {2}", 
                        find, str1, result1)
      Console.WriteLine()
      Console.WriteLine("Using CompareInfo.IndexOf(string, char) method")
      Console.WriteLine("the result of searching for {0} in the string {1} is: {2}", 
                        find, str2, result2)
      Console.WriteLine()
      Console.WriteLine("Using CompareInfo.IndexOf(string, char, CompareOptions) method")
      Console.WriteLine("the result of searching for {0} in the string {1} is: {2}", 
                        find, str1, result3)
      Console.WriteLine()
      Console.WriteLine("Using CompareInfo.IndexOf(string, char, CompareOptions) method")
      Console.WriteLine("the result of searching for {0} in the string {1} is: {2}", 
                        find, str2, result4)
   End Sub
End Class
' The example displays the following output:
'    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
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);
   }
}
// The example displays the following output:
//    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 パラメーターを受け取る CompareInfo.IndexOf メソッドと、このパラメーターを受け取らない同一メソッドは異なる結果を取得します。 CompareInfo.IndexOf メソッドによって実行されるカルチャに依存した比較では、文字 "Æ" がこの文字を構成する 2 文字 "ae" と等価であると評価されます。 CompareInfo.IndexOf メソッドによって実行される序数 (カルチャを認識しない) 比較では、文字 "Æ" と "ae" の Unicode コード値が一致しないため、これらの値が等価であるとは評価されません。

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

Imports System.Globalization
Imports System.IO
Imports System.Threading

Public Class TextToFile   
   Public Shared Sub Main()
      ' Creates and initializes a new array to store 
      ' these date/time objects.
      Dim stringArray() As String = { "Apple", "Æble", "Zebra"}

      ' Displays the values of the array.
      Console.WriteLine("The original string array:")
      PrintIndexAndValues(stringArray)

      ' Set the CurrentCulture to "en-US".
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      ' Sort the values of the Array.
      Array.Sort(stringArray)

      ' Display the values of the array.
      Console.WriteLine("After sorting for the ""en-US"" culture:")
      PrintIndexAndValues(stringArray)

      ' Set 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("After sorting for the culture ""da-DK"":")
      PrintIndexAndValues(stringArray)
   End Sub

   Public Shared Sub PrintIndexAndValues(myArray() As String)
      For i As Integer = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
         Console.WriteLine("[{0}]: {1}", i, myArray(i))
      Next
      Console.WriteLine()
   End Sub 
End Class
' The example displays the following output:
'       The original string array:
'       [0]: Apple
'       [1]: Æble
'       [2]: Zebra
'       
'       After sorting for the "en-US" culture:
'       [0]: Æble
'       [1]: Apple
'       [2]: Zebra
'       
'       After sorting for the culture "da-DK":
'       [0]: Apple
'       [1]: Zebra
'       [2]: Æble
using System;
using System.Globalization;
using System.Threading;

public class ArraySort 
{
   public static void Main(String[] args) 
   {
      // Create and initialize a new array to store the strings.
      string[] stringArray = { "Apple", "Æble", "Zebra"};

      // Display the values of the array.
      Console.WriteLine( "The original string array:");
      PrintIndexAndValues(stringArray);

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

      // Display the values of the array.
      Console.WriteLine("After sorting for the culture \"en-US\":");
      PrintIndexAndValues(stringArray); 

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

      // Display the values of the array.
      Console.WriteLine("After sorting for the culture \"da-DK\":");
      PrintIndexAndValues(stringArray); 
   }
   public static void PrintIndexAndValues(string[] myArray)  
   {
      for (int i = myArray.GetLowerBound(0); i <= 
            myArray.GetUpperBound(0); i++ )
         Console.WriteLine("[{0}]: {1}", i, myArray[i]);
      Console.WriteLine();      
   }
}
// The example displays the following output:
//       The original string array:
//       [0]: Apple
//       [1]: Æble
//       [2]: Zebra
//       
//       After sorting for the "en-US" culture:
//       [0]: Æble
//       [1]: Apple
//       [2]: Zebra
//       
//       After sorting for the culture "da-DK":
//       [0]: Apple
//       [1]: Zebra
//       [2]: Æble

並べ替えキーの使用

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

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

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

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

Imports System.Globalization
Imports System.Threading

Public Class SortKeySample
   Public Shared Sub Main()
      Dim str1 As [String] = "Apple"
      Dim str2 As [String] = "Æble"

      ' Set the CurrentCulture to "da-DK".
      Dim dk As New CultureInfo("da-DK")
      Thread.CurrentThread.CurrentCulture = dk

      ' Create 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)

      ' Compare the two sort keys and display the results.
      Dim result1 As Integer = SortKey.Compare(sc1, sc2)
      Console.WriteLine("When the  current culture is ""da-DK"",")
      Console.WriteLine("the result of comparing {0} with {1} is: {2}", 
                        str1, str2, result1)
      Console.WriteLine()

      ' Set the CurrentCulture to "en-US".
      Dim enus As New CultureInfo("en-US")
      Thread.CurrentThread.CurrentCulture = enus

      ' Create 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)

      ' Compare the two sort keys and display the results.
      Dim result2 As Integer = SortKey.Compare(sc3, sc4)
      Console.WriteLine("When the CurrentCulture is ""en-US"",")
      Console.WriteLine("the result of comparing {0} with {1} is: {2}", 
                        str1, str2, result2)
   End Sub
End Class
' The example displays the following output:
'       When the  current culture 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
using System;
using System.Threading;
using System.Globalization;

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

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

      // Create 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);

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

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

      // Create 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);

      // Compare the two sort keys and display the results.
      int result2 = SortKey.Compare(sc3, sc4);
      Console.WriteLine("When the CurrentCulture is \"en-US\",");
      Console.WriteLine("the result of comparing {0} with {1} is: {2}", 
                        str1, str2, result2);
   }
}
// The example displays the following output:
//       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 が特定の正規化形式を強制的に適用したり、保証したりすることはありません。 開発者が各自の責任で、開発するアプリケーションで適切な正規化を実行します。

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

参照

概念

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

正規化と並べ替え

その他の技術情報

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