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

データの並べ替えと順序付けの規則はカルチャによって異なります。 たとえば、並べ替え順序で大文字小文字が区別される場合と区別されない場合があります。 並べ替え順序は文字の発音や文字の視覚的表現に基づくことがあります。 東アジア圏の言語では、文字が表意文字の画数と部首によって並べ替えられます。 また、並べ替えは、言語やカルチャで使用されているアルファベットの順序によっても異なります。 たとえば、デンマーク語の文字 "Æ" は、アルファベットでは "Z" の後に位置します。 国際対応アプリケーションでは、カルチャ固有および言語固有の並べ替え規則をサポートするため、カルチャ別にデータの比較と並べ替えを実行できる必要があります。

注意

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

文字列の比較

CompareInfo クラスには、カルチャに依存した文字列比較を実行するためのメソッドが含まれています。 CultureInfo クラスには、CompareInfo クラスのインスタンスを取得する CompareInfo プロパティがあります。 CompareInfo オブジェクトによって、特定のカルチャでの文字列の比較方法と並べ替え方法が定義されます。 String.Compare メソッドは、カルチャの CompareInfo オブジェクトの情報を使用して文字列を比較します。

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

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 (中国語 - 中国) などのカルチャでは、発音による並べ替え (既定) と画数による並べ替えの 2 つの並べ替え順序がサポートされています。 カルチャの名前 (zh-CN など) を使用して CultureInfo オブジェクトを作成する場合は、既定の並べ替え順序が使用されます。 代替の並べ替え順序を指定するには、CultureInfo.CultureInfo(Int32) コンストラクターまたは CultureInfo.CultureInfo(Int32, Boolean) コンストラクターを呼び出し、代替の並べ替え順序の識別子を使用して 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 メソッドを呼び出して、指定された文字列内の文字または部分文字列のインデックスを取得できます。このインデックスはゼロから始まります。 文字または部分文字列が見つからない場合、このメソッドは -1 を返します。 指定文字を検索する場合、CompareOptions 型のパラメーターを受け取る IndexOf オーバーロードとこのパラメーターを受け取らないメソッド オーバーロードでは、実行される比較が異なることがあります。 このパラメーターを使用しないメソッド オーバーロードは、カルチャに依存した、大文字と小文字を区別する検索を実行します。 たとえば、Unicode 値が "Æ" (\u00C6) のような合字 (複数文字で構成されている 1 文字) を表す場合、カルチャによっては、"AE" (\u0041\u0045) のように 2 つの文字が並んでいるのと同じと見なされることがあります。 正確な Unicode 値の序数 (カルチャに依存しない) 検索を実行するには、CompareOptions 型のパラメーターを受け取り、そのパラメーターを Ordinal に設定する CompareInfo.IndexOf オーバーロードのいずれかを使用します。

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

カルチャによって CompareInfo.IndexOf メソッドが返す結果が異なることを次の例に示します。 たとえば、デンマーク語 (デンマーク) のカルチャと英語 (米国) のカルチャの CultureInfo オブジェクトを作成し、CompareInfo.IndexOf メソッドのオーバーロードを使用して文字列 "æble" と "aeble" 内の文字 "æ" を検索します。 デンマーク語 (デンマーク) のカルチャでは、CompareInfo.IndexOf(String, Char) メソッドと、CompareOptions.Ordinal の比較オプションがある CompareInfo.IndexOf(String, Char, CompareOptions) メソッドは、それぞれの文字列で同じ値を返します。 つまり、文字 "æ" は Unicode 値 \u00E6 のみと等価であると見なされます。 英語 (米国) のカルチャで文字列 "aeble" 内の "æ" を検索すると、2 つのオーバーロードが返す結果は異なります。 つまり、CompareInfo.IndexOf(String, Char) メソッドで実行されるカルチャに依存した比較では、文字 "æ" はこの文字を構成する "a" と "e" と等価であると評価されます。

Imports System.Globalization
Imports System.Threading

Public Class Example
   Public Shared Sub Main()
      Dim str1 As String = "æble"
      Dim str2 As String = "aeble"
      Dim find As Char = "æ"c

      ' Create CultureInfo objects representing the Danish (Denmark)
      ' and English (United States) cultures.
      Dim cultures() As CultureInfo = { CultureInfo.CreateSpecificCulture("da-DK"), 
                                        CultureInfo.CreateSpecificCulture("en-US") }

      For Each ci In cultures
         Thread.CurrentThread.CurrentCulture = ci

         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("The current culture is {0}", 
                           CultureInfo.CurrentCulture.Name)
         Console.WriteLine()
         Console.WriteLine("   CompareInfo.IndexOf(string, char) method:")
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str1, result1)
         Console.WriteLine()
         Console.WriteLine("   CompareInfo.IndexOf(string, char) method:")
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str2, result2)
         Console.WriteLine()
         Console.WriteLine("   CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method")
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str1, result3)
         Console.WriteLine()
         Console.WriteLine("   CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method")
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str2, result4)
         Console.WriteLine()
      Next
   End Sub
End Class
' The example displays the following output:
'    The current culture is da-DK
'    
'       CompareInfo.IndexOf(string, char) method:
'       Position of æ in the string æble: 0
'    
'       CompareInfo.IndexOf(string, char) method:
'       Position of æ in the string aeble: -1
'    
'       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
'       Position of æ in the string æble: 0
'    
'       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
'       Position of æ in the string aeble: -1
'    
'    The current culture is en-US
'    
'       CompareInfo.IndexOf(string, char) method:
'       Position of æ in the string æble: 0
'    
'       CompareInfo.IndexOf(string, char) method:
'       Position of æ in the string aeble: 0
'    
'       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
'       Position of æ in the string æble: 0
'    
'       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
'       Position of æ in the string aeble: -1
using System;
using System.Globalization;
using System.Threading;

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

      // Create CultureInfo objects representing the Danish (Denmark)
      // and English (United States) cultures.
      CultureInfo[] cultures = { CultureInfo.CreateSpecificCulture("da-DK"), 
                                 CultureInfo.CreateSpecificCulture("en-US") };

      foreach (var ci in cultures) {
         Thread.CurrentThread.CurrentCulture = ci;

         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("\nThe current culture is {0}", 
                           CultureInfo.CurrentCulture.Name);
         Console.WriteLine("\n   CompareInfo.IndexOf(string, char) method:");
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str1, result1);

         Console.WriteLine("\n   CompareInfo.IndexOf(string, char) method:");
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str2, result2);

         Console.WriteLine("\n   CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method");
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str1, result3);

         Console.WriteLine("\n   CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method");
         Console.WriteLine("   Position of {0} in the string {1}: {2}", 
                           find, str2, result4);
         Console.WriteLine();
      }   
   }
}
// The example displays the following output
//    The current culture is da-DK
//    
//       CompareInfo.IndexOf(string, char) method:
//       Position of æ in the string æble: 0
//    
//       CompareInfo.IndexOf(string, char) method:
//       Position of æ in the string aeble: -1
//    
//       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
//       Position of æ in the string æble: 0
//    
//       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
//       Position of æ in the string aeble: -1
//    
//    
//    The current culture is en-US
//    
//       CompareInfo.IndexOf(string, char) method:
//       Position of æ in the string æble: 0
//    
//       CompareInfo.IndexOf(string, char) method:
//       Position of æ in the string aeble: 0
//    
//       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
//       Position of æ in the string æble: 0
//    
//       CompareInfo.IndexOf(string, char, CompareOptions.Ordinal) method
//       Position of æ in the string aeble: -1

文字列の並べ替え

Array.Sort メソッドのオーバーロードを使用して、現在のカルチャに基づいて配列を並べ替えることができます。 次の例では、3 つの文字列から成る配列を作成します。 最初に、System.Threading.Thread.CurrentThread.CurrentCulture プロパティを en-US に設定し、Array.Sort(Array) メソッドを呼び出します。 これよって、英語 (米国) カルチャの並べ替え規則に基づく並べ替え順序が適用されます。 次に、System.Threading.Thread.CurrentThread.CurrentCulture プロパティを da-DK に設定し、再度 Array.Sort メソッドを呼び出します。 適用される並べ替え順序が en-US の並べ替え順序と異なる点に注意してください。これは、デンマーク語 (デンマーク) の並べ替え規則が使用されるためです。

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

並べ替えキーの使用

.NET Framework では、並べ替えキーを使用して、カルチャに依存した並べ替え操作をサポートします。 文字列内の各文字には、アルファベット順、大文字と小文字の区別、発音の区別など、さまざまなカテゴリの並べ替えウェイトが指定されます。 並べ替えキーは、特定の文字列に対するこれらのウェイトを格納するリポジトリとして使用されます。 たとえば、並べ替えキーにはアルファベット順ウェイトの文字列、大文字小文字のウェイトの文字列などが特定の順序で格納されています。 並べ替えキーの詳細については、「Unicode Technical Standard #10: Unicode Collation Algorithm (Unicode 技術標準 #10: Unicode 照合順序アルゴリズム)」の Unicode 標準を参照してください。

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

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

CurrentCulture プロパティが da-DK に設定されている場合に 2 つの文字列 (str1 と str2) の並べ替えキーを作成する例を次に示します。 SortKey.Compare メソッドを使用して 2 つの文字列が比較され、比較結果が表示されます。 このメソッドは、str1 が str2 より小さい場合は負の整数値、str1 と str2 が等しい場合は 0、str1 が str2 より大きい場合は正の整数値を返します。 次に、System.Threading.Thread.CurrentThread.CurrentCulture プロパティを en-US に設定し、同じ文字列に対して新しい並べ替えキーを作成する例を示します。 この例では、並べ替えキーを比較して結果を表示します。 並べ替え結果が現在のカルチャの設定によって異なる点に注意してください。 次の例の実行結果は、前述の「文字列の比較」の例の結果と同じですが、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 はすべての Unicode 正規化形式をサポートしているので、特定の正規化形式を強制的に適用したり、保証したりすることはありません。 開発者が各自の責任で、アプリケーションに適した正規化を選択します。

文字列の正規化の詳細については、String クラスに関するトピックの「正規化」を参照してください。

参照

概念

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

その他の技術情報

.NET Framework アプリケーションのグローバライズとローカライズ