StringComparer StringComparer StringComparer StringComparer Class

定義

大文字と小文字の区別、およびカルチャ ベースまたは序数ベースの比較規則を使用する文字列比較操作を表します。Represents a string comparison operation that uses specific case and culture-based or ordinal comparison rules.

public ref class StringComparer abstract : System::Collections::Generic::IComparer<System::String ^>, System::Collections::Generic::IEqualityComparer<System::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
    interface IEqualityComparer
    interface IComparer<string>
    interface IEqualityComparer<string>
Public MustInherit Class StringComparer
Implements IComparer, IComparer(Of String), IEqualityComparer, IEqualityComparer(Of String)
継承
StringComparerStringComparerStringComparerStringComparer
属性
実装

次の例では、プロパティ、およびCreateのメソッド、StringComparerクラス。The following example demonstrates the properties and the Create method of the StringComparer class. 例に示すさまざまなStringComparerオブジェクト並べ替え 3 つのバージョン、ラテン語の文字は。The example illustrates how different StringComparer objects sort three versions of the Latin letter 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 code example demonstrates members of the System.StringComparer class.

Imports System
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 'Main
    
    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 'Display
End Class 'Sample '

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

注釈

派生したオブジェクト、StringComparerクラスは、文字列ベースの比較、等しいかどうか、および両方のケースとカルチャ固有の比較規則を考慮するハッシュ コードの操作を具体化します。An object derived from the StringComparer class embodies string-based comparison, equality, and hash code operations that take into account both case and culture-specific comparison rules. 使用することができます、StringComparerジェネリック コレクションの要素を並べ替えるため型固有の比較を作成するクラス。You can use the StringComparer class to create a type-specific comparison to sort the elements in a generic collection. などのクラスHashtableDictionary<TKey,TValue>SortedList、およびSortedList<TKey,TValue>を使用して、StringComparer並べ替えを行う際のクラス。Classes such as Hashtable, Dictionary<TKey,TValue>, SortedList, and SortedList<TKey,TValue> use the StringComparer class for sorting purposes.

比較演算で表される、StringComparer大文字であるか、大文字と小文字をいずれかの単語 (カルチャ) を使用して、クラスが定義されているまたは序数に基づく (カルチャに依存しない) 比較ルール。A comparison operation that is represented by the StringComparer class is defined to be either case-sensitive or case-insensitive, and use either word (culture-sensitive) or ordinal (culture-insensitive) comparison rules. 単語および序数に基づく比較の規則の詳細については、次を参照してください。System.Globalization.CompareOptionsします。For more information about word and ordinal comparison rules, see System.Globalization.CompareOptions.

注意

Windows オペレーティング システムの並べ替え操作と比較操作で使用される文字の重みに関する情報を含む一連のテキスト ファイルである並べ替え重みテーブル と、Linux と macOS 用の並べ替え重みテーブルの最新バージョンである デフォルト Unicode 照合基本テーブルをダウンロードできます。You can download the Sorting Weight Tables, a set of text files that contain information on the character weights used in sorting and comparison operations for Windows operating systems, and the Default Unicode Collation Element Table, the latest version of the sort weight table for Linux and macOS. Linux と macOS での並べ替え重みのテーブルの特定のバージョンは、システムにインストールされている International Components for Unicode ライブラリのバージョンによって異なります。The specific version of the sort weight table on Linux and macOS depends on the version of the International Components for Unicode libraries installed on the system. 実装される ICU のバージョンと Unicode のバージョンに関する情報は、ICU のダウンロードに関する記事を参照してください。For information on ICU versions and the Unicode versions that they implement, see Downloading ICU.

実装されたプロパティImplemented properties

使用する方法の詳細については混乱する必要があります、StringComparerクラス上の矛盾が原因のプロパティ。You might be confused about how to use the StringComparer class properties because of a seeming contradiction. StringComparerクラスが宣言されているabstract(MustInherit Visual basic)、つまり、そのメンバーがから派生したクラスのオブジェクトでのみ呼び出すことができます、StringComparerクラス。The StringComparer class is declared abstract (MustInherit in Visual Basic), which means its members can be invoked only on an object of a class derived from the StringComparer class. この処理は、の各プロパティ、StringComparerクラスが宣言されているstatic(Shared Visual Basic で)、つまり、プロパティが派生クラスを作成しなくても呼び出すことができます。The contradiction is that each property of the StringComparer class is declared static (Shared in Visual Basic), which means the property can be invoked without first creating a derived class.

呼び出すことができます、StringComparerプロパティを直接各プロパティが実際には匿名から派生したクラスのインスタンスを返すため、StringComparerクラス。You can call a StringComparer property directly because each property actually returns an instance of an anonymous class that is derived from the StringComparer class. 各プロパティ値の型は、その結果、 StringComparer、いない匿名のクラスの基底クラスは、匿名の型クラス自体。Consequently, the type of each property value is StringComparer, which is the base class of the anonymous class, not the type of the anonymous class itself. StringComparerクラスのプロパティが返す、StringComparer定義済みの場合と比較規則をサポートするオブジェクト。Each StringComparer class property returns a StringComparer object that supports predefined case and comparison rules.

コンストラクター

StringComparer() StringComparer() StringComparer() StringComparer()

StringComparer クラスの新しいインスタンスを初期化します。Initializes a new instance of the StringComparer class.

プロパティ

CurrentCulture CurrentCulture CurrentCulture CurrentCulture

現在のカルチャの単語ベースの比較規則を使用して、大文字と小文字を区別して文字列を比較する StringComparer オブジェクトを取得します。Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the current culture.

CurrentCultureIgnoreCase CurrentCultureIgnoreCase CurrentCultureIgnoreCase CurrentCultureIgnoreCase

現在のカルチャの単語ベースの比較規則を使用して、大文字と小文字を区別せずに文字列を比較する StringComparer オブジェクトを取得します。Gets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture.

InvariantCulture InvariantCulture InvariantCulture InvariantCulture

インバリアント カルチャの単語ベースの比較規則を使用して、大文字と小文字を区別して文字列を比較する StringComparer オブジェクトを取得します。Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture.

InvariantCultureIgnoreCase InvariantCultureIgnoreCase InvariantCultureIgnoreCase InvariantCultureIgnoreCase

インバリアント カルチャの単語ベースの比較規則を使用して、大文字と小文字を区別せずに文字列を比較する StringComparer オブジェクトを取得します。Gets a StringComparer object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture.

Ordinal Ordinal Ordinal Ordinal

大文字と小文字を区別して序数の文字列比較を実行する StringComparer オブジェクトを取得します。Gets a StringComparer object that performs a case-sensitive ordinal string comparison.

OrdinalIgnoreCase OrdinalIgnoreCase OrdinalIgnoreCase OrdinalIgnoreCase

大文字と小文字を区別せずに序数の文字列比較を実行する StringComparer オブジェクトを取得します。Gets a StringComparer object that performs a case-insensitive ordinal string comparison.

メソッド

Compare(Object, Object) Compare(Object, Object) Compare(Object, Object) Compare(Object, Object)

派生クラスでオーバーライドされた場合、2 つのオブジェクトを比較し、相対的な並べ替え順序を表す値を返します。When overridden in a derived class, compares two objects and returns an indication of their relative sort order.

Compare(String, String) Compare(String, String) Compare(String, String) Compare(String, String)

派生クラスでオーバーライドされた場合、2 つの文字列を比較し、相対的な並べ替え順序を表す値を返します。When overridden in a derived class, compares two strings and returns an indication of their relative sort order.

Create(CultureInfo, Boolean) Create(CultureInfo, Boolean) Create(CultureInfo, Boolean) Create(CultureInfo, Boolean)

指定したカルチャの規則に基づいて文字列を比較する StringComparer オブジェクトを作成します。Creates a StringComparer object that compares strings according to the rules of a specified culture.

Create(CultureInfo, CompareOptions) Create(CultureInfo, CompareOptions) Create(CultureInfo, CompareOptions) Create(CultureInfo, CompareOptions)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Equals(Object, Object) Equals(Object, Object) Equals(Object, Object) Equals(Object, Object)

派生クラスでオーバーライドされた場合、2 つのオブジェクトが等しいかどうかを示します。When overridden in a derived class, indicates whether two objects are equal.

Equals(String, String) Equals(String, String) Equals(String, String) Equals(String, String)

派生クラスでオーバーライドされた場合、2 つの文字列が等しいかどうかを示します。When overridden in a derived class, indicates whether two strings are equal.

FromComparison(StringComparison) FromComparison(StringComparison) FromComparison(StringComparison) FromComparison(StringComparison)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetHashCode(Object) GetHashCode(Object) GetHashCode(Object) GetHashCode(Object)

派生クラスでオーバーライドされた場合、指定したオブジェクトのハッシュ コードを取得します。When overridden in a derived class, gets the hash code for the specified object.

GetHashCode(String) GetHashCode(String) GetHashCode(String) GetHashCode(String)

派生クラスでオーバーライドされた場合、指定した文字列のハッシュ コードを取得します。When overridden in a derived class, gets the hash code for the specified string.

GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

明示的なインターフェイスの実装

IComparer.Compare(Object, Object) IComparer.Compare(Object, Object) IComparer.Compare(Object, Object) IComparer.Compare(Object, Object)
IEqualityComparer.Equals(Object, Object) IEqualityComparer.Equals(Object, Object) IEqualityComparer.Equals(Object, Object) IEqualityComparer.Equals(Object, Object)
IEqualityComparer.GetHashCode(Object) IEqualityComparer.GetHashCode(Object) IEqualityComparer.GetHashCode(Object) IEqualityComparer.GetHashCode(Object)

適用対象

こちらもご覧ください