StringComparer 클래스

정의

특정 대/소문자 및 문화권 기반 또는 서수 비교 규칙을 사용하는 문자열 비교 연산을 나타냅니다.

public ref class StringComparer abstract : System::Collections::Generic::IComparer<System::String ^>, System::Collections::Generic::IEqualityComparer<System::String ^>, System::Collections::IComparer, System::Collections::IEqualityComparer
public abstract class StringComparer : System.Collections.Generic.IComparer<string>, System.Collections.Generic.IEqualityComparer<string>, System.Collections.IComparer, System.Collections.IEqualityComparer
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class StringComparer : System.Collections.Generic.IComparer<string>, System.Collections.Generic.IEqualityComparer<string>, System.Collections.IComparer, System.Collections.IEqualityComparer
type StringComparer = class
    interface IComparer<string>
    interface IEqualityComparer<string>
    interface IComparer
    interface IEqualityComparer
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type StringComparer = class
    interface IComparer
    interface IEqualityComparer
    interface IComparer<string>
    interface IEqualityComparer<string>
Public MustInherit Class StringComparer
Implements IComparer, IComparer(Of String), IEqualityComparer, IEqualityComparer(Of String)
상속
StringComparer
특성
구현

예제

다음 예제에서는 속성 및 클래스의 Create 메서드입니다 StringComparer . 이 예제에서는 서로 다른 StringComparer 개체가 라틴 문자 I의 세 가지 버전을 정렬하는 방법을 보여 줍니다.

// This example demonstrates members of the
// System::StringComparer class.

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Globalization;
using namespace System::Threading;

void Display(List<String^>^ stringList, String^ title)
{
    Char firstChar;
    int codePoint;
    Console::WriteLine(title);
    for each (String^ s in stringList)
    {
        firstChar = s[0];
        codePoint = Convert::ToInt32(firstChar);
        Console::WriteLine("0x{0:x}", codePoint);
    }
    Console::WriteLine();
}

int main()
{
    // Create a list of string.
    List<String^>^ stringList = gcnew List<String^>();

    // Get the tr-TR (Turkish-Turkey) culture.
    CultureInfo^ turkishCulture = gcnew CultureInfo("tr-TR");

    // Get the culture that is associated with the current thread.
    CultureInfo^ currentCulture = Thread::CurrentThread->CurrentCulture;

    // Get the standard StringComparers.
    StringComparer^ invariant = StringComparer::InvariantCulture;
    StringComparer^ invariantIgnoreCase =
        StringComparer::InvariantCultureIgnoreCase;
    StringComparer^ current = StringComparer::CurrentCulture;
    StringComparer^ currentIgnoreCase =
        StringComparer::CurrentCultureIgnoreCase;
    StringComparer^ ordinal = StringComparer::Ordinal;
    StringComparer^ ordinalIgnoreCase = StringComparer::OrdinalIgnoreCase;

    // Create a StringComparer that uses the Turkish culture and ignores
    // case.
    StringComparer^ turkishIgnoreCase =
        StringComparer::Create(turkishCulture, true);

    // Define three strings consisting of different versions of the
    // letter I. LATIN CAPITAL LETTER I (U+0049)
    String^ capitalLetterI = "I";

    // LATIN SMALL LETTER I (U+0069)
    String^ smallLetterI = "i";

    // LATIN SMALL LETTER DOTLESS I (U+0131)
    String^ smallLetterDotlessI = L"\u0131";

    // Add the three strings to the list.
    stringList->Add(capitalLetterI);
    stringList->Add(smallLetterI);
    stringList->Add(smallLetterDotlessI);

    // Display the original list order.
    Display(stringList, "The original order of the list entries...");

    // Sort the list using the invariant culture.
    stringList->Sort(invariant);
    Display(stringList, "Invariant culture...");
    stringList->Sort(invariantIgnoreCase);
    Display(stringList, "Invariant culture, ignore case...");

    // Sort the list using the current culture.
    Console::WriteLine("The current culture is \"{0}\".",
        currentCulture->Name);
    stringList->Sort(current);
    Display(stringList, "Current culture...");
    stringList->Sort(currentIgnoreCase);
    Display(stringList, "Current culture, ignore case...");

    // Sort the list using the ordinal value of the character code points.
    stringList->Sort(ordinal);
    Display(stringList, "Ordinal...");
    stringList->Sort(ordinalIgnoreCase);
    Display(stringList, "Ordinal, ignore case...");

    // Sort the list using the Turkish culture, which treats LATIN SMALL
    // LETTER DOTLESS I differently than LATIN SMALL LETTER I.
    stringList->Sort(turkishIgnoreCase);
    Display(stringList, "Turkish culture, ignore case...");
}
/*
This code example produces the following results:

The original order of the list entries...
0x49
0x69
0x131

Invariant culture...
0x69
0x49
0x131

Invariant culture, ignore case...
0x49
0x69
0x131

The current culture is "en-US".
Current culture...
0x69
0x49
0x131

Current culture, ignore case...
0x49
0x69
0x131

Ordinal...
0x49
0x69
0x131

Ordinal, ignore case...
0x69
0x49
0x131

Turkish culture, ignore case...
0x131
0x49
0x69

*/
// This example demonstrates members of the 
// System.StringComparer class.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
        // Create a list of string.
        List<string> list = new List<string>();

        // Get the tr-TR (Turkish-Turkey) culture.
        CultureInfo turkish = new CultureInfo("tr-TR");

        // Get the culture that is associated with the current thread.
        CultureInfo thisCulture = Thread.CurrentThread.CurrentCulture;

        // Get the standard StringComparers.
        StringComparer invCmp =   StringComparer.InvariantCulture;
        StringComparer invICCmp = StringComparer.InvariantCultureIgnoreCase;
        StringComparer currCmp = StringComparer.CurrentCulture;
        StringComparer currICCmp = StringComparer.CurrentCultureIgnoreCase;
        StringComparer ordCmp = StringComparer.Ordinal;
        StringComparer ordICCmp = StringComparer.OrdinalIgnoreCase;

        // Create a StringComparer that uses the Turkish culture and ignores case.
        StringComparer turkICComp = StringComparer.Create(turkish, true);

        // Define three strings consisting of different versions of the letter I.
        // LATIN CAPITAL LETTER I (U+0049)
        string capitalLetterI = "I";  

        // LATIN SMALL LETTER I (U+0069)
        string smallLetterI   = "i";

        // LATIN SMALL LETTER DOTLESS I (U+0131)
        string smallLetterDotlessI = "\u0131";

        // Add the three strings to the list.
        list.Add(capitalLetterI);
        list.Add(smallLetterI);
        list.Add(smallLetterDotlessI);

        // Display the original list order.
        Display(list, "The original order of the list entries...");

        // Sort the list using the invariant culture.
        list.Sort(invCmp);
        Display(list, "Invariant culture...");
        list.Sort(invICCmp);
        Display(list, "Invariant culture, ignore case...");

        // Sort the list using the current culture.
        Console.WriteLine("The current culture is \"{0}\".", thisCulture.Name);
        list.Sort(currCmp);
        Display(list, "Current culture...");
        list.Sort(currICCmp);
        Display(list, "Current culture, ignore case...");

        // Sort the list using the ordinal value of the character code points.
        list.Sort(ordCmp);
        Display(list, "Ordinal...");
        list.Sort(ordICCmp);
        Display(list, "Ordinal, ignore case...");

        // Sort the list using the Turkish culture, which treats LATIN SMALL LETTER 
        // DOTLESS I differently than LATIN SMALL LETTER I.
        list.Sort(turkICComp);
        Display(list, "Turkish culture, ignore case...");
    }

    public static void Display(List<string> lst, string title)
    {
        Char c;
        int  codePoint;
        Console.WriteLine(title);
        foreach (string s in lst)
        {
            c = s[0];
            codePoint = Convert.ToInt32(c);
            Console.WriteLine("0x{0:x}", codePoint); 
        }
        Console.WriteLine();
    }
}
/*
This code example produces the following results:

The original order of the list entries...
0x49
0x69
0x131

Invariant culture...
0x69
0x49
0x131

Invariant culture, ignore case...
0x49
0x69
0x131

The current culture is "en-US".
Current culture...
0x69
0x49
0x131

Current culture, ignore case...
0x49
0x69
0x131

Ordinal...
0x49
0x69
0x131

Ordinal, ignore case...
0x69
0x49
0x131

Turkish culture, ignore case...
0x131
0x49
0x69

*/
// This example demonstrates members of the 
// System.StringComparer class.

open System
open System.Globalization
open System.Threading

let display (lst: ResizeArray<string>) title =
    printfn $"%s{title}"
    for s in lst do
        let c = s[0]
        let codePoint = Convert.ToInt32 c
        printfn $"0x{codePoint:x}"
    printfn ""

// Create a list of string.
let list = ResizeArray()

// Get the tr-TR (Turkish-Turkey) culture.
let turkish = CultureInfo "tr-TR"

// Get the culture that is associated with the current thread.
let thisCulture = Thread.CurrentThread.CurrentCulture

// Get the standard StringComparers.
let invCmp =   StringComparer.InvariantCulture
let invICCmp = StringComparer.InvariantCultureIgnoreCase
let currCmp = StringComparer.CurrentCulture
let currICCmp = StringComparer.CurrentCultureIgnoreCase
let ordCmp = StringComparer.Ordinal
let ordICCmp = StringComparer.OrdinalIgnoreCase

// Create a StringComparer that uses the Turkish culture and ignores case.
let turkICComp = StringComparer.Create(turkish, true)

// Define three strings consisting of different versions of the letter I.
// LATIN CAPITAL LETTER I (U+0049)
let capitalLetterI = "I"  

// LATIN SMALL LETTER I (U+0069)
let smallLetterI   = "i"

// LATIN SMALL LETTER DOTLESS I (U+0131)
let smallLetterDotlessI = "\u0131"

// Add the three strings to the list.
list.Add capitalLetterI
list.Add smallLetterI
list.Add smallLetterDotlessI

// Display the original list order.
display list "The original order of the list entries..."

// Sort the list using the invariant culture.
list.Sort invCmp
display list "Invariant culture..."
list.Sort invICCmp
display list "Invariant culture, ignore case..."

// Sort the list using the current culture.
printfn $"The current culture is \"{thisCulture.Name}\"."
list.Sort currCmp
display list "Current culture..."
list.Sort currICCmp
display list "Current culture, ignore case..."

// Sort the list using the ordinal value of the character code points.
list.Sort ordCmp
display list "Ordinal..."
list.Sort ordICCmp
display list "Ordinal, ignore case..."

// Sort the list using the Turkish culture, which treats LATIN SMALL LETTER 
// DOTLESS I differently than LATIN SMALL LETTER I.
list.Sort turkICComp
display list "Turkish culture, ignore case..."


(*
This code example produces the following results:

The original order of the list entries...
0x49
0x69
0x131

Invariant culture...
0x69
0x49
0x131

Invariant culture, ignore case...
0x49
0x69
0x131

The current culture is "en-US".
Current culture...
0x69
0x49
0x131

Current culture, ignore case...
0x49
0x69
0x131

Ordinal...
0x49
0x69
0x131

Ordinal, ignore case...
0x69
0x49
0x131

Turkish culture, ignore case...
0x131
0x49
0x69

*)
' This code example demonstrates members of the System.StringComparer class.

Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading

Class Sample
    
    Public Shared Sub Main() 
        ' Create a list of string.
        Dim list As New List(Of String) 
        
        ' Get the tr-TR (Turkish-Turkey) culture.
        Dim turkish As New CultureInfo("tr-TR")
        
        ' Get the culture that is associated with the current thread.
        Dim thisCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
        
        ' Get the standard StringComparers.
        Dim invCmp As StringComparer = StringComparer.InvariantCulture
        Dim invICCmp As StringComparer = StringComparer.InvariantCultureIgnoreCase
        Dim currCmp As StringComparer = StringComparer.CurrentCulture
        Dim currICCmp As StringComparer = StringComparer.CurrentCultureIgnoreCase
        Dim ordCmp As StringComparer = StringComparer.Ordinal
        Dim ordICCmp As StringComparer = StringComparer.OrdinalIgnoreCase
        
        ' Create a StringComparer that uses the Turkish culture and ignores case.
        Dim turkICComp As StringComparer = StringComparer.Create(turkish, True)
        
        ' Define three strings consisting of different versions of the letter I.
        ' LATIN CAPITAL LETTER I (U+0049)
        Dim capitalLetterI As String = "I"
        
        ' LATIN SMALL LETTER I (U+0069)
        Dim smallLetterI As String = "i"
        
        ' LATIN SMALL LETTER DOTLESS I (U+0131)
        Dim smallLetterDotlessI As String = "ı"
        
        ' Add the three strings to the list.
        list.Add(capitalLetterI)
        list.Add(smallLetterI)
        list.Add(smallLetterDotlessI)
        
        ' Display the original list order.
        Display(list, "The original order of the list entries...")
        
        ' Sort the list using the invariant culture.
        list.Sort(invCmp)
        Display(list, "Invariant culture...")
        list.Sort(invICCmp)
        Display(list, "Invariant culture, ignore case...")
        
        ' Sort the list using the current culture.
        Console.WriteLine("The current culture is ""{0}"".", thisCulture.Name)
        list.Sort(currCmp)
        Display(list, "Current culture...")
        list.Sort(currICCmp)
        Display(list, "Current culture, ignore case...")
        
        ' Sort the list using the ordinal value of the character code points.
        list.Sort(ordCmp)
        Display(list, "Ordinal...")
        list.Sort(ordICCmp)
        Display(list, "Ordinal, ignore case...")
        
        ' Sort the list using the Turkish culture, which treats LATIN SMALL LETTER 
        ' DOTLESS I differently than LATIN SMALL LETTER I.
        list.Sort(turkICComp)
        Display(list, "Turkish culture, ignore case...")
    
    End Sub
    
    Public Shared Sub Display(ByVal lst As List(Of String), ByVal title As String)
        Dim c As Char
        Dim s As String
        Dim codePoint As Integer

        Console.WriteLine(title)
        For Each s In lst
            c = s(0)
            codePoint = Convert.ToInt32(c)
            Console.WriteLine("0x{0:x}", codePoint)
        Next s
        Console.WriteLine()
    End Sub
End Class

'This code example produces the following results:
'
'The original order of the list entries...
'0x49
'0x69
'0x131
'
'Invariant culture...
'0x69
'0x49
'0x131
'
'Invariant culture, ignore case...
'0x49
'0x69
'0x131
'
'The current culture is "en-US".
'Current culture...
'0x69
'0x49
'0x131
'
'Current culture, ignore case...
'0x49
'0x69
'0x131
'
'Ordinal...
'0x49
'0x69
'0x131
'
'Ordinal, ignore case...
'0x69
'0x49
'0x131
'
'Turkish culture, ignore case...
'0x131
'0x49
'0x69
'

설명

이 API에 대한 자세한 내용은 StringComparer에 대한 추가 API 설명을 참조하세요.

생성자

StringComparer()

StringComparer 클래스의 새 인스턴스를 초기화합니다.

속성

CurrentCulture

현재 문화권의 단어 비교 규칙을 사용하여 대/소문자를 구분하는 문자열을 비교하는 StringComparer 개체를 가져옵니다.

CurrentCultureIgnoreCase

현재 문화권의 단어 비교 규칙을 사용하여 대/소문자를 구분하지 않는 문자열을 비교하는 StringComparer 개체를 가져옵니다.

InvariantCulture

고정 문화권의 단어 비교 규칙을 사용하여 대/소문자를 구분하는 문자열 비교를 수행하는 StringComparer 개체를 가져옵니다.

InvariantCultureIgnoreCase

고정 문화권의 단어 비교 규칙을 사용하여 대/소문자를 구분하지 않는 문자열 비교를 수행하는 StringComparer 개체를 가져옵니다.

Ordinal

대/소문자를 구분하는 서수 문자열 비교를 수행하는 StringComparer 개체를 가져옵니다.

OrdinalIgnoreCase

대/소문자를 구분하지 않는 서수 문자열 비교를 수행하는 StringComparer 개체를 가져옵니다.

메서드

Compare(Object, Object)

파생 클래스에서 재정의된 경우 두 개체를 비교하고 상대적 정렬 순서를 나타내는 값을 반환합니다.

Compare(String, String)

파생 클래스에서 재정의된 경우 두 문자열을 비교하고 상대적 정렬 순서를 나타내는 값을 반환합니다.

Create(CultureInfo, Boolean)

지정된 문화권의 규칙에 따라 문자열을 비교하는 StringComparer 개체를 만듭니다.

Create(CultureInfo, CompareOptions)

지정된 문화권 및 문자열 옵션 규칙에 따라 문자열을 비교하는 StringComparer 개체를 만듭니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
Equals(Object, Object)

파생 클래스에서 재정의된 경우 두 개체가 같음을 나타냅니다.

Equals(String, String)

파생 클래스에서 재정의된 경우 두 문자열이 같음을 나타냅니다.

FromComparison(StringComparison)

지정된 StringComparison 인스턴스를 StringComparer 인스턴스로 변환합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetHashCode(Object)

파생 클래스에서 재정의된 경우 지정된 개체의 해시 코드를 가져옵니다.

GetHashCode(String)

파생 클래스에서 재정의된 경우 지정된 문자열의 해시 코드를 가져옵니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsWellKnownCultureAwareComparer(IEqualityComparer<String>, CompareInfo, CompareOptions)

지정된 IEqualityComparer<T> 가 잘 알려진 문화권 인식 문자열 비교자인지 여부를 확인합니다.

IsWellKnownOrdinalComparer(IEqualityComparer<String>, Boolean)

지정된 IEqualityComparer<T> 가 잘 알려진 서수 문자열 비교자인지 여부를 확인합니다.

MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

IComparer.Compare(Object, Object)

두 개체를 비교하여 한 개체가 다른 개체보다 작거나, 같거나 또는 크다는 것을 나타내는 값을 반환합니다.

IEqualityComparer.Equals(Object, Object)

지정한 개체가 같은지 여부를 확인합니다.

IEqualityComparer.GetHashCode(Object)

지정한 개체의 해시 코드를 반환합니다.

적용 대상

추가 정보