StringComparer Class
Represents a string comparison operation that uses specific case and culture-based or ordinal comparison rules.
Assembly: mscorlib (in mscorlib.dll)
The StringComparer type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() ![]() | CurrentCulture | Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the current culture. |
![]() ![]() ![]() ![]() | CurrentCultureIgnoreCase | Gets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture. |
![]() ![]() ![]() | InvariantCulture | Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture. |
![]() ![]() ![]() | InvariantCultureIgnoreCase | Gets a StringComparer object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture. |
![]() ![]() ![]() ![]() | Ordinal | Gets a StringComparer object that performs a case-sensitive ordinal string comparison. |
![]() ![]() ![]() ![]() | OrdinalIgnoreCase | Gets a StringComparer object that performs a case-insensitive ordinal string comparison. |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | Compare(Object, Object) | When overridden in a derived class, compares two objects and returns an indication of their relative sort order. |
![]() ![]() ![]() | Compare(String, String) | When overridden in a derived class, compares two strings and returns an indication of their relative sort order. |
![]() ![]() ![]() ![]() | Create | Creates a StringComparer object that compares strings according to the rules of a specified culture. |
![]() ![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() ![]() | Equals(Object, Object) | When overridden in a derived class, indicates whether two objects are equal. |
![]() ![]() ![]() | Equals(String, String) | When overridden in a derived class, indicates whether two strings are equal. |
![]() ![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() ![]() | GetHashCode() | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() ![]() | GetHashCode(Object) | When overridden in a derived class, gets the hash code for the specified object. |
![]() ![]() ![]() | GetHashCode(String) | When overridden in a derived class, gets the hash code for the specified string. |
![]() ![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
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. You can use the StringComparer class to create a type-specific comparison to sort the elements in a generic collection. Classes such as Hashtable, Dictionary<TKey, TValue>, SortedList, and SortedList<TKey, TValue> use the StringComparer class for sorting purposes.
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. For more information about word and ordinal comparison rules, see System.Globalization::CompareOptions.
Implemented Properties
You might be confused about how to use the StringComparer class properties because of a seeming contradiction. 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. 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.
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. 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. Each StringComparer class property returns a StringComparer object that supports predefined case and comparison rules.
The following code example demonstrates the properties and the Create method of the StringComparer class. 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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.





