英語で読む

次の方法で共有


StringComparer.Create メソッド

定義

オーバーロード

Create(CultureInfo, Boolean)

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

Create(CultureInfo, CompareOptions)

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

Create(CultureInfo, Boolean)

ソース:
StringComparer.cs
ソース:
StringComparer.cs
ソース:
StringComparer.cs

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

C#
public static StringComparer Create (System.Globalization.CultureInfo culture, bool ignoreCase);

パラメーター

culture
CultureInfo

文字列比較を実行するための言語的な規則を定義したカルチャ。

ignoreCase
Boolean

比較操作で大文字と小文字を区別しない場合は、true を指定します。比較操作で大文字と小文字を区別する場合は、false を指定します。

戻り値

StringComparer パラメーターで使用する比較規則と culture パラメーターで指定する大文字と小文字の区別の規則に基づいて文字列を比較する新しい ignoreCase オブジェクト。

例外

culturenullです。

次のコード例では、 クラスのプロパティと メソッドをCreateStringComparer示します。 この例では、さまざまな 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

*/

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 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 2.0, 2.1

Create(CultureInfo, CompareOptions)

ソース:
StringComparer.cs
ソース:
StringComparer.cs
ソース:
StringComparer.cs

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

C#
public static StringComparer Create (System.Globalization.CultureInfo culture, System.Globalization.CompareOptions options);

パラメーター

culture
CultureInfo

文字列比較を実行するための言語的な規則を定義したカルチャ。

options
CompareOptions

CompareOptions 値のビットごとの組み合わせ。

戻り値

culture パラメーターで使用する比較規則と、指定した options パラメーターに基づいて文字列を比較する新しい StringComparer オブジェクト。

例外

culturenullです。

options に無効なフラグが含まれています。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1