StringComparer Osztály

Definíció

Egy sztring-összehasonlító műveletet jelöl, amely konkrét eset- és kultúraalapú vagydinális összehasonlítási szabályokat használ.

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)
Öröklődés
StringComparer
Attribútumok
Megvalósítás

Példák

Az alábbi példa az osztály tulajdonságait és Create metódusát StringComparer mutatja be. A példa bemutatja, hogy a különböző StringComparer objektumok hogyan rendezik az I. latin betű három verzióját.

// 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
'

Megjegyzések

Az osztályból StringComparer származó objektumok sztringalapú összehasonlítási, egyenlőségi és kivonatkód-műveleteket tartalmaznak, amelyek figyelembe veszik az eset- és kultúraspecifikus összehasonlító szabályokat is. Az StringComparer osztály használatával típusspecifikus összehasonlítást hozhat létre az általános gyűjtemény elemeinek rendezéséhez. Az olyan osztályok, mint például Hashtablea , Dictionary<TKey,TValue>, SortedListés SortedList<TKey,TValue> az StringComparer osztályt rendezési célokra használják.

Az StringComparer osztály által képviselt összehasonlító művelet lehet kis- és nagybetűket megkülönböztető vagy nem megkülönböztető, valamint használhat szó alapú (kultúraérzékeny) vagy sorszámos (kultúrafüggetlen) összehasonlítási szabályokat. A szó- és a sorszám-összehasonlító szabályokkal kapcsolatos további információkért lásd: System.Globalization.CompareOptions.

Megjegyzés:

Letöltheti az Alapértelmezett Unicode összerendelési elem táblázatot, amely a súlytáblázat legújabb verziója. A rendezési súlytábla adott verziója a rendszeren telepített Unicode-kódtárak nemzetközi összetevőinek verziójától függ. Az ICU-verziókról és az általuk implementálható Unicode-verziókról további információt az ICU letöltése című témakörben talál.

Implementált tulajdonságok

Előfordulhat, hogy egy látszólag ellentmondás miatt nem tudja használni az StringComparer osztálytulajdonságokat. Az StringComparer osztály deklarálva abstract van (MustInherit a Visual Basicben), ami azt jelenti, hogy tagjai csak az osztályból StringComparer származtatott osztály objektumán hívhatók meg. Az ellentmondás az, hogy az StringComparer osztály minden tulajdonsága deklarálva static van (Shared a Visual Basicben), ami azt jelenti, hogy a tulajdonság egy származtatott osztály létrehozása nélkül hívható meg.

Közvetlenül meghívhat egy tulajdonságot StringComparer , mert minden tulajdonság egy névtelen osztály egy példányát adja vissza, amely az StringComparer osztályból származik. Következésképpen az egyes tulajdonságértékek StringComparertípusa a névtelen osztály alaposztálya, nem pedig maga a névtelen osztály típusa. Minden StringComparer osztálytulajdonság egy StringComparer olyan objektumot ad vissza, amely támogatja az előre meghatározott eset- és összehasonlító szabályokat.

Konstruktorok

Name Description
StringComparer()

Inicializálja a StringComparer osztály új példányát.

Tulajdonságok

Name Description
CurrentCulture

Lekéri a StringComparer kis- és nagybetűket megkülönböztető sztring-összehasonlítást végző objektumot az aktuális kultúra szóösszehasonlásának szabályaival.

CurrentCultureIgnoreCase

Lekéri a StringComparer kis- és nagybetűket érzéketlen sztringek összehasonlítását végző objektumot az aktuális kultúra szóösszehasonlító szabályaival.

InvariantCulture

Lekéri a StringComparer kis- és nagybetűket megkülönböztető sztring-összehasonlítást végző objektumot az invariáns kultúra szóösszehasonlító szabályaival.

InvariantCultureIgnoreCase

Beolvas egy StringComparer objektumot, amely kis- és nagybetűk érzéketlen sztring-összehasonlítását hajtja végre az invariáns kultúra szóösszehasonlító szabályaival.

Ordinal

Lekéri a StringComparer kis- és nagybetűket megkülönböztető sorszámú sztringek összehasonlítását végző objektumot.

OrdinalIgnoreCase

Beolvas egy StringComparer objektumot, amely kis- és nagybetűk érzéketlen sorszámú sztring-összehasonlítását hajtja végre.

Metódusok

Name Description
Compare(Object, Object)

Ha egy származtatott osztályban felül van bírálva, két objektumot hasonlít össze, és a relatív rendezési sorrendjük jelzését adja vissza.

Compare(String, String)

Ha egy származtatott osztályban felül van bírálva, két sztringet hasonlít össze, és a relatív rendezési sorrendjük jelzését adja vissza.

Create(CultureInfo, Boolean)

Létrehoz egy StringComparer objektumot, amely egy adott kultúra szabályai szerint hasonlítja össze a sztringeket.

Create(CultureInfo, CompareOptions)

Létrehoz egy StringComparer objektumot, amely a megadott kultúra és sztringbeállítások szabályainak megfelelően hasonlítja össze a sztringeket.

Equals(Object, Object)

Ha egy származtatott osztályban felül van bírálva, azt jelzi, hogy két objektum egyenlő-e.

Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
Equals(String, String)

Ha egy származtatott osztályban felül van bírálva, azt jelzi, hogy két sztring egyenlő-e.

FromComparison(StringComparison)

A megadott StringComparison példányt példánysá StringComparer alakítja.

GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetHashCode(Object)

Ha felül van bírálva egy származtatott osztályban, lekéri a megadott objektum kivonatkódját.

GetHashCode(String)

Ha felül van bírálva egy származtatott osztályban, lekéri a megadott sztring kivonatkódját.

GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
IsWellKnownCultureAwareComparer(IEqualityComparer<String>, CompareInfo, CompareOptions)

Meghatározza, hogy a megadott IEqualityComparer<T> érték egy jól ismert kultúratudatos sztring-összehasonlító-e.

IsWellKnownOrdinalComparer(IEqualityComparer<String>, Boolean)

Meghatározza, hogy a megadott IEqualityComparer<T> érték jól ismert sorszámú sztring-összehasonlító-e.

MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

Explicit interfész-implementációk

Name Description
IComparer.Compare(Object, Object)

Két objektumot hasonlít össze, és egy értéket ad vissza, amely azt jelzi, hogy az egyik kisebb, egyenlő vagy nagyobb a másiknál.

IEqualityComparer.Equals(Object, Object)

Meghatározza, hogy a megadott objektumok egyenlőek-e.

IEqualityComparer.GetHashCode(Object)

A megadott objektum kivonatkódját adja vissza.

A következőre érvényes:

Lásd még