英語で読む

次の方法で共有


StringComparer クラス

定義

大文字と小文字の区別、およびカルチャ ベースまたは序数ベースの特定の比較規則を使用する文字列比較操作を表します。

C#
public abstract class StringComparer : System.Collections.Generic.IComparer<string>, System.Collections.Generic.IEqualityComparer<string>, System.Collections.IComparer, System.Collections.IEqualityComparer
C#
[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
継承
StringComparer
属性
実装

次の例では、 クラスのプロパティと Create メソッドを StringComparer 示します。 この例では、異なる StringComparer オブジェクトがラテン文字 I の 3 つのバージョンを並べ替える方法を示しています。

C#
// 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

*/

注釈

この API の詳細については、「 StringComparer の補足 API 解説」を参照してください。

コンストラクター

StringComparer()

StringComparer クラスの新しいインスタンスを初期化します。

プロパティ

CurrentCulture

現在のカルチャの単語ベースの比較規則を使用して、大文字と小文字を区別して文字列を比較する StringComparer オブジェクトを取得します。

CurrentCultureIgnoreCase

現在のカルチャの単語ベースの比較規則を使用して、大文字と小文字を区別せずに文字列を比較する StringComparer オブジェクトを取得します。

InvariantCulture

インバリアント カルチャの単語ベースの比較規則を使用して、大文字と小文字を区別して文字列を比較する StringComparer オブジェクトを取得します。

InvariantCultureIgnoreCase

インバリアント カルチャの単語ベースの比較規則を使用して、大文字と小文字を区別せずに文字列を比較する StringComparer オブジェクトを取得します。

Ordinal

大文字と小文字を区別して序数の文字列比較を実行する StringComparer オブジェクトを取得します。

OrdinalIgnoreCase

大文字と小文字を区別せずに序数の文字列比較を実行する StringComparer オブジェクトを取得します。

メソッド

Compare(Object, Object)

派生クラスでオーバーライドされた場合、2 つのオブジェクトを比較し、相対的な並べ替え順序を表す値を返します。

Compare(String, String)

派生クラスでオーバーライドされた場合、2 つの文字列を比較し、相対的な並べ替え順序を表す値を返します。

Create(CultureInfo, Boolean)

指定したカルチャの規則に基づいて文字列を比較する StringComparer オブジェクトを作成します。

Create(CultureInfo, CompareOptions)

指定したカルチャおよび文字列オプションの規則に基づいて文字列を比較する StringComparer オブジェクトを作成します。

Equals(Object, Object)

派生クラスでオーバーライドされた場合、2 つのオブジェクトが等しいかどうかを示します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Equals(String, String)

派生クラスでオーバーライドされた場合、2 つの文字列が等しいかどうかを示します。

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)

2 つのオブジェクトを比較して、一方が他方より小さいか、同じか、または大きいかを示す値を返します。

IEqualityComparer.Equals(Object, Object)

指定したオブジェクトが等しいかどうかを判断します。

IEqualityComparer.GetHashCode(Object)

指定したオブジェクトのハッシュ コードを返します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください