StringComparer Class

 

Represents a string comparison operation that uses specific case and culture-based or ordinal comparison rules.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

System.Object
  System.StringComparer

<SerializableAttribute>
<ComVisibleAttribute(True)>
Public MustInherit Class StringComparer
	Implements IComparer, IEqualityComparer, IComparer(Of String),
	IEqualityComparer(Of String)

NameDescription
System_CAPS_protmethodStringComparer()

Initializes a new instance of the StringComparer class.

NameDescription
System_CAPS_pubpropertySystem_CAPS_staticCurrentCulture

Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the current culture.

System_CAPS_pubpropertySystem_CAPS_staticCurrentCultureIgnoreCase

Gets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture.

System_CAPS_pubpropertySystem_CAPS_staticInvariantCulture

Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture.

System_CAPS_pubpropertySystem_CAPS_staticInvariantCultureIgnoreCase

Gets a StringComparer object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture.

System_CAPS_pubpropertySystem_CAPS_staticOrdinal

Gets a StringComparer object that performs a case-sensitive ordinal string comparison.

System_CAPS_pubpropertySystem_CAPS_staticOrdinalIgnoreCase

Gets a StringComparer object that performs a case-insensitive ordinal string comparison.

NameDescription
System_CAPS_pubmethodCompare(Object, Object)

When overridden in a derived class, compares two objects and returns an indication of their relative sort order.

System_CAPS_pubmethodCompare(String, String)

When overridden in a derived class, compares two strings and returns an indication of their relative sort order.

System_CAPS_pubmethodSystem_CAPS_staticCreate(CultureInfo, Boolean)

Creates a StringComparer object that compares strings according to the rules of a specified culture.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodEquals(Object, Object)

When overridden in a derived class, indicates whether two objects are equal.

System_CAPS_pubmethodEquals(String, String)

When overridden in a derived class, indicates whether two strings are equal.

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetHashCode(Object)

When overridden in a derived class, gets the hash code for the specified object.

System_CAPS_pubmethodGetHashCode(String)

When overridden in a derived class, gets the hash code for the specified string.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

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(Of TKey, TValue), SortedList, and SortedList(Of 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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: