StringComparer クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public MustInherit Class StringComparer Implements IComparer, IEqualityComparer, IComparer(Of String), _ IEqualityComparer(Of String)
[SerializableAttribute] [ComVisibleAttribute(true)] public abstract class StringComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class StringComparer abstract : IComparer, IEqualityComparer, IComparer<String^>, IEqualityComparer<String^>

StringComparer クラスから派生したオブジェクトでは、大文字と小文字の区別および特定のカルチャ固有の比較規則を考慮して、文字列ベースの比較操作、値の比較操作、およびハッシュ コード操作を実行できます。StringComparer クラスを使用して、型固有の比較機能を作成し、ジェネリック コレクションで要素をソートできます。Hashtable、Dictionary、SortedList、SortedList などのクラスでは、StringComparer クラスを使用してソートします。
StringComparer クラスで表す比較操作には、大文字と小文字を区別するかどうか、単語ベース (カルチャに依存) と序数ベース (カルチャに依存しない) のどちらの比較規則を使用するかを定義できます。単語ベースおよび序数ベースの比較規則の詳細については、System.Globalization.CompareOptions のトピックを参照してください。
実装するプロパティ外観上の矛盾により、StringComparer クラスのプロパティの使用方法がわかりにくい場合があります。StringComparer クラスは、abstract (Visual Basic の場合は MustInherit) として宣言されます。そのため、このクラスのメンバは、StringComparer クラスから派生したクラスのオブジェクトだけに対して呼び出すことができます。対照的に、StringComparer クラスの各プロパティは、static (Visual Basic の場合は Shared) として宣言されます。そのため、先に派生クラスを作成しなくてもプロパティを呼び出すことができます。
実際には、各プロパティからは、StringComparer クラスから派生した匿名クラスのインスタンスが返るため、StringComparer プロパティを直接呼び出すことができます。そのため、各プロパティ値の型は、StringComparer になります。この型は、匿名クラス自体の型ではなく、匿名クラスの基本クラスです。各 StringComparer クラス プロパティは、大文字と小文字の区別や比較に関する定義済みの規則をサポートする StringComparer オブジェクトを返します。

StringComparer クラスのプロパティおよび Create メソッドを使ったコード例を次に示します。この例は、StringComparer オブジェクトによって 3 種類のラテン文字「I」がそれぞれどのように並べ替えられるかを示しています。
' 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 '
// 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. 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 */

System.StringComparer


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringComparer コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringComparer プロパティ
StringComparer メソッド

名前 | 説明 | |
---|---|---|
![]() | Compare | オーバーロードされます。 2 つのオブジェクトまたは文字列を比較し、相対的な並べ替え順序を表す値を返します。 |
![]() | Create | 指定したカルチャの規則に基づいて文字列を比較する StringComparer オブジェクトを作成します。 |
![]() | Equals | オーバーロードされます。 派生クラスでオーバーライドされた場合、2 つのオブジェクトまたは文字列が等しいかどうかを示します。 |
![]() | GetHashCode | オーバーロードされます。 派生クラスでオーバーライドされた場合、現在の StringComparer オブジェクトのハッシュ コードを取得します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

StringComparer メンバ
大文字と小文字の区別、およびカルチャ ベースまたは序数ベースの比較規則を使用する文字列比較操作を表します。
StringComparer データ型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Compare | オーバーロードされます。 2 つのオブジェクトまたは文字列を比較し、相対的な並べ替え順序を表す値を返します。 |
![]() | Create | 指定したカルチャの規則に基づいて文字列を比較する StringComparer オブジェクトを作成します。 |
![]() | Equals | オーバーロードされます。 派生クラスでオーバーライドされた場合、2 つのオブジェクトまたは文字列が等しいかどうかを示します。 |
![]() | GetHashCode | オーバーロードされます。 派生クラスでオーバーライドされた場合、現在の StringComparer オブジェクトのハッシュ コードを取得します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からStringComparerを検索する場合は、下記のリンクをクリックしてください。

- StringComparerのページへのリンク