다음을 통해 공유


특정 문화권의 데이터 비교 및 정렬

사전순 및 항목의 순서 지정할 때 사용하는 규칙은 문화권마다 다릅니다. 예를 들어, 정렬 순서를 정할 때 대/소문자를 구분할 수도 있고 구분하지 않을 수도 있습니다. 이러한 정렬 순서는 음성학을 따르거나 문자 모양에 따라 결정될 수 있습니다. 동아시아 언어에서는 표의 문자의 부수와 획에 따라 정렬됩니다. 언어와 문화권이 알파벳에 사용하는 기본 순서에 따라 정렬 순서가 달라질 수도 있습니다. 예를 들어 스웨덴어 알파벳의 "Æ" 문자는 "Z" 다음에 옵니다. 독일어에도 이 문자가 있지만 "ae"로 간주되어 알파벳에서 "A" 다음에 정렬됩니다. 지역화 대비 응용 프로그램에서 문화권별 및 언어별 정렬 규칙을 지원하려면 각 문화권에 따라 데이터를 비교 및 정렬할 수 있어야 합니다.

참고   문화권 구분 동작이 불필요한 경우도 있습니다. 문화권을 구분하지 않는 작업을 수행해야 하는 때와 방법에 대한 자세한 내용은 문화권을 구분하지 않는 문자열 작업을 참조하십시오.

문자열 비교

CompareInfo 클래스에서 제공하는 메서드 집합을 통해 문화권을 구분하여 문자열을 비교할 수 입니다. CultureInfo 클래스에는 이 클래스의 인스턴스인 CompareInfo 속성이 있습니다. 이 속성은 특정 문화권의 문자열을 비교하여 정렬하는 방법을 정의합니다. String.Compare 메서드에서는 CompareInfo 속성의 정보를 사용하여 문자열을 비교합니다. String.Compare 메서드는 string1이 string2보다 작으면 음의 정수를, string1과 string2가 서로 같으면 0을, string1이 string2보다 크면 양의 정수를 반환합니다.

다음 코드 예제에서는 비교하는 데 사용되는 문화권에 따라 String.Compare 메서드로 두 문자열을 비교한 결과가 달라짐을 보여 줍니다. 우선 CurrentCulture를 덴마크어(덴마크) 문화권을 나타내는 da-DK로 설정하고 문자열 "Apple"과 "Æble"을 비교합니다. 덴마크어에서는 문자 "Æ"가 개별 문자로 취급되며 알파벳에서 "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" 중국어(PRC) 문화권에서는 발음에 의한 정렬(기본값)과 획수에 의한 정렬을 지원합니다. 응용 프로그램에서 "zh-CN"과 같은 문화권 이름을 사용하여 CultureInfo 개체를 만드는 경우 기본 정렬 순서가 사용됩니다. 대체 정렬 순서를 지정하려면 응용 프로그램에서 대체 정렬 순서의 ID를 사용하여 CultureInfo 개체를 만들어야 합니다. 그런 다음 CompareInfo에서 문자열 비교에 사용할 CompareInfo 개체를 가져옵니다. 또는 응용 프로그램에서 CompareInfo.GetCompareInfo 메서드를 사용하고 대체 정렬 순서의 ID를 지정하여 CompareInfo 개체를 직접 만들 수도 있습니다.

다음은 대체 정렬 순서를 지원하는 문화권과 기본 및 대체 정렬 순서에 대한 ID입니다.

문화권 이름

문화권

기본 정렬 이름 및 ID

대체 정렬 이름 및 ID

es-ES

스페인어(스페인)

국제: 0x00000C0A

전통: 0x0000040A

zh-TW

중국어(대만)

획수: 0x00000404

보포모포: 0x00030404

zh-CN

중국어(중국)

발음: 0x00000804

획수: 0x00020804

zh-HK

중국어(홍콩 특별 행정구)

획수: 0x00000c04

획수: 0x00020c04

zh-SG

중국어(싱가포르)

발음: 0x00001004

획수: 0x00021004

zh-MO

중국어(마카오 특별 행정구)

발음: 0x00001404

획수: 0x00021404

ja-JP

일본어(일본)

기본: 0x00000411

유니코드: 0x00010411

ko-KR

한국어(대한민국)

기본: 0x00000412

한국어 완성 - 유니코드: 0x00010412

de-DE

독일어(독일)

사전: 0x00000407

전화 번호부 정렬 DIN: 0x00010407

hu-HU

헝가리어(헝가리)

기본: 0x0000040e

기술 정렬: 0x0001040e

ka-GE

그루지야어(그루지야)

전통: 0x00000437

현대 정렬: 0x00010437

문자열 검색

응용 프로그램에서는 오버로드된 CompareInfo.IndexOf 메서드를 사용하여 지정된 문자열에서 특정 문자 또는 하위 문자열의 인덱스(0부터 시작)를 검색할 수 있습니다. 지정된 문자열에 해당 문자나 하위 문자열이 없으면 이 메서드는 음의 정수를 검색합니다. 응용 프로그램에서 CompareInfo.IndexOf를 사용하여 지정된 문자를 검색할 때는 CompareOptions 매개 변수를 받는 메서드 오버로드와 이 매개 변수를 받지 않는 메서드 오버로드의 비교 동작이 서로 다르다는 점을 고려해야 합니다. 문자 형식을 검색하고 CompareOptions 매개 변수를 받지 않는 메서드 오버로드는 문화권 구분 검색을 수행합니다. 즉, 유니코드 값이 합자 "Æ"(\u00C6)와 같은 미리 구성된 문자를 나타내는 경우 문화권에 따라 "AE"(\u0041\u0045)와 같이 해당 구성 요소가 올바른 순서로 나오는 경우와 같은 것으로 처리될 수 있습니다. 문화권을 구분하지 않는, 즉 유니코드 값이 같은 경우에만 문자 형식이 다른 문자 형식과 같은 것으로 간주되는 서수 검색을 수행하려면 응용 프로그램에서 CompareInfo.IndexOf 오버로드 중 CompareOptions 매개 변수를 받는 오버로드를 사용하고 이 매개 변수를 Ordinal 값으로 설정합니다.

응용 프로그램에서 String.IndexOf 메서드의 오버로드 중 특정 문자를 검색하는 오버로드를 사용하여 문화권을 구분하지 않는 서수 검색을 수행할 수도 있습니다. 이 메서드의 오버로드 중 문자열을 검색하는 오버로드는 문화권 구분 검색을 수행합니다.

다음 코드 예제에서는 IndexOf 메서드가 문화권에 따라 다른 결과를 검색함을 보여 줍니다. 우선 덴마크어(덴마크) 문화권을 나타내는 "da-DK"에 대한 CultureInfo 개체를 만듭니다. 그런 다음 CompareInfo.IndexOf 메서드의 오버로드를 사용하여 문자열 "Æble"과 "aeble"에서 문자 "Æ"를 검색합니다. "da-DK" 문화권의 경우 Ordinal로 설정된 CompareOptions 매개 변수를 받는 CompareInfo.IndexOf 메서드와 이 매개 변수를 받지 않는 동일한 메서드에서 같은 내용을 검색합니다. 문자 "Æ"는 오로지 유니코드 코드 값 \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")로 바꾸면 CompareOptions 매개 변수가 Ordinal로 설정된 CompareInfo.IndexOf 메서드와 이 매개 변수가 없는 같은 메서드가 검색하는 결과가 서로 다릅니다. CompareInfo.IndexOf 메서드에서 수행하는 문화권 구분 비교에서는 문자 "Æ"가 해당 구성 요소인 "ae"와 같은 것으로 평가됩니다. CompareInfo.IndexOf 메서드에서 수행하는 문화권을 구분하지 않는 서수 비교에서는 해당 유니코드 코드 값이 일치하지 않으므로 문자 "Æ"가 "ae"와 동일한 것으로 검색되지 않습니다.

영어(미국)를 나타내는 "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 속성에 따라 배열을 정렬할 수 있습니다. 다음 예제에서는 세 문자열의 배열이 만들어집니다. 우선 Thread.CurrentThread.CurrentCulture 속성을 "en-US"로 설정하고 Array.Sort 메서드를 호출합니다. 결과 정렬 순서는 영어(미국) 문화권의 정렬 규칙을 따릅니다. 다음으로 Thread.CurrentThread.CurrentCulture 속성을 da-DK로 설정하고 Array.Sort 메서드를 다시 호출합니다. da-DK의 정렬 규칙을 사용하는 경우 결과 정렬 순서가 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

정렬 키 사용

정렬 키는 문화권에 따른 정렬 방식을 지원하는 데 사용됩니다. 유니코드 표준에 따라 문자열의 각 문자에는 사전순, 대/소문자 및 분음 부호의 가중치를 포함한 몇 가지 정렬 가중치 범주가 부여됩니다. 정렬 키는 특정 문자열에 대해 이러한 가중치의 리포지토리 역할을 합니다. 예를 들어, 문자열 사전순 가중치 다음에 문자열 대/소문자 가중치를 적용하는 방식으로 정렬 키에 가중치를 포함할 수 있습니다. 정렬 키 개념에 대한 자세한 내용은 유니코드 홈 페이지에서 "The Unicode Standard"를 참조하십시오.

.NET Framework에서 SortKey 클래스는 문자열을 해당 정렬 키로, 또는 정렬 키를 해당 문자열로 매핑합니다. 응용 프로그램에서는 CompareInfo.GetSortKey 메서드를 사용하여 지정한 문자열에 대한 정렬 키를 만들 수 있습니다. 이렇게 만들어진 지정된 문자열에 대한 정렬 키는 지정된 CurrentCultureCompareOptions 값에 따라 달라질 수 있는 바이트 시퀀스입니다. 예를 들어 응용 프로그램에서 정렬 키를 만들 때 IgnoreCase 값을 지정하면 해당 정렬 키를 사용하는 문자열 비교 작업에서 대/소문자가 구분되지 않습니다.

응용 프로그램에서는 문자열에 대한 정렬 키를 만들어 SortKey 클래스에서 제공하는 메서드에 매개 변수로 전달할 수 있습니다. SortKey.Compare 메서드를 사용하여 정렬 키를 비교할 수 있습니다. 이 메서드는 단순히 바이트 단위로 비교하므로 String.Compare를 사용하는 것보다 훨씬 빠릅니다. 응용 프로그램에서 정렬 작업을 많이 수행하는 경우 사용되는 모든 문자열에 대한 정렬 키를 생성하여 저장하면 성능을 향상시킬 수 있습니다. 정렬이나 비교 작업을 수행해야 할 때 응용 프로그램에서 문자열 대신 정렬 키를 사용할 수 있습니다.

다음 코드 예제에서는 CurrentCulture가 da-DK로 설정된 상태에서 두 문자열의 정렬 키를 만듭니다. 그런 다음 SortKey.Compare 메서드를 사용하여 두 문자열을 비교하고 결과를 표시합니다. 이 메서드는 string1이 string2보다 작으면 음의 정수를, string1과 string2가 서로 같으면 0을, string1이 string2보다 크면 양의 정수를 반환합니다. 다음으로 Thread.CurrentThread.CurrentCulture 속성을 en-US로 설정하고 같은 문자열에 대한 정렬 키를 만듭니다. 문자열의 정렬 키를 비교하고 결과를 표시합니다. 정렬 결과는 CurrentCulture의 설정에 따라 다릅니다. 다음 예제의 결과는 이 항목 앞부분에 나온 문자열 비교 예제에서 이러한 문자열을 비교한 결과와 같지만, String.Compare 메서드를 사용할 때보다 SortKey.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에서는 특정 표준화 양식을 보장하거나 강요하지 않으므로 개발하는 응용 프로그램에 적합한 표준화를 실행하는 것은 개발자 책임입니다.

문자열 정규화에 대한 자세한 내용은 정규화 및 정렬을 참조하십시오.

참고 항목

개념

문화권을 구분하지 않는 문자열 작업

정규화 및 정렬

기타 리소스

인코딩 및 지역화