Edit

Share via


StringComparer Class

Definition

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

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
Inheritance
StringComparer
Attributes
Implements

Examples

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.

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

*/

Remarks

For more information about this API, see Supplemental API remarks for StringComparer.

Constructors

StringComparer()

Initializes a new instance of the StringComparer class.

Properties

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.

Methods

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(CultureInfo, Boolean)

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

Create(CultureInfo, CompareOptions)

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

Equals(Object, Object)

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

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Equals(String, String)

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

FromComparison(StringComparison)

Converts the specified StringComparison instance to a StringComparer instance.

GetHashCode()

Serves as the default hash function.

(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)
IsWellKnownCultureAwareComparer(IEqualityComparer<String>, CompareInfo, CompareOptions)

Determines whether the specified IEqualityComparer<T> is a well-known culture-aware string comparer.

IsWellKnownOrdinalComparer(IEqualityComparer<String>, Boolean)

Determines whether the specified IEqualityComparer<T> is a well-known ordinal string comparer.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IComparer.Compare(Object, Object)

Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

IEqualityComparer.Equals(Object, Object)

Determines whether the specified objects are equal.

IEqualityComparer.GetHashCode(Object)

Returns a hash code for the specified object.

Applies to

Product Versions
.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

See also