Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

StringComparer Class

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

System.Object
  System.StringComparer

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

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class StringComparer : IComparer, 
	IEqualityComparer, IComparer<string>, IEqualityComparer<string>

The StringComparer type exposes the following members.

  NameDescription
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsStringComparerInitializes a new instance of the StringComparer class.
Top

  NameDescription
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCurrentCultureGets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the current culture.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCurrentCultureIgnoreCaseGets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture.
Public propertyStatic memberSupported by the XNA FrameworkInvariantCultureGets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture.
Public propertyStatic memberSupported by the XNA FrameworkInvariantCultureIgnoreCaseGets a StringComparer object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOrdinalGets a StringComparer object that performs a case-sensitive ordinal string comparison.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOrdinalIgnoreCaseGets a StringComparer object that performs a case-insensitive ordinal string comparison.
Top

  NameDescription
Public methodSupported by the XNA FrameworkCompare(Object, Object)When overridden in a derived class, compares two objects and returns an indication of their relative sort order.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCompare(String, String)When overridden in a derived class, compares two strings and returns an indication of their relative sort order.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCreateCreates a StringComparer object that compares strings according to the rules of a specified culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkEquals(Object, Object)When overridden in a derived class, indicates whether two objects are equal.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(String, String)When overridden in a derived class, indicates whether two strings are equal.
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCode()Serves as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCode(Object)When overridden in a derived class, gets the hash code for the specified object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCode(String)When overridden in a derived class, gets the hash code for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsIComparer.CompareCompares two objects and returns a value that indicates whether one is less than, equal to, or greater than the other.
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsIEqualityComparer.EqualsDetermines whether the specified objects are equal.
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsIEqualityComparer.GetHashCodeReturns a hash code for the specified object.
Top

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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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