This topic has not yet been rated - Rate this topic

CompareInfo Class

Implements a set of methods for culture-sensitive string comparisons.

System.Object
  System.Globalization.CompareInfo

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class CompareInfo : IDeserializationCallback

The CompareInfo type exposes the following members.

  Name Description
Public property Supported by the XNA Framework LCID Gets the properly formed culture identifier for the current CompareInfo.
Public property Supported by the XNA Framework Supported by Portable Class Library Name Gets the name of the culture used for sorting operations by this CompareInfo object.
Top
  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library Compare(String, String) Compares two strings.
Public method Supported by the XNA Framework Supported by Portable Class Library Compare(String, String, CompareOptions) Compares two strings using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library Compare(String, Int32, String, Int32) Compares the end section of a string with the end section of another string.
Public method Supported by the XNA Framework Supported by Portable Class Library Compare(String, Int32, String, Int32, CompareOptions) Compares the end section of a string with the end section of another string using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library Compare(String, Int32, Int32, String, Int32, Int32) Compares a section of one string with a section of another string.
Public method Supported by the XNA Framework Supported by Portable Class Library Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) Compares a section of one string with a section of another string using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals Determines whether the specified object is equal to the current CompareInfo object. (Overrides Object.Equals(Object).)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member Supported by the XNA Framework GetCompareInfo(Int32) Initializes a new CompareInfo object that is associated with the culture with the specified identifier.
Public method Static member GetCompareInfo(String) Initializes a new CompareInfo object that is associated with the culture with the specified name.
Public method Static member GetCompareInfo(Int32, Assembly) Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.
Public method Static member GetCompareInfo(String, Assembly) Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for the current CompareInfo for hashing algorithms and data structures, such as a hash table. (Overrides Object.GetHashCode().)
Public method GetSortKey(String) Gets the sort key for the specified string.
Public method GetSortKey(String, CompareOptions) Gets a SortKey object for the specified string using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, Char) Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, String) Searches for the specified substring and returns the zero-based index of the first occurrence within the entire source string.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, Char, CompareOptions) Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string using the specified CompareOptions value.
Public method IndexOf(String, Char, Int32) Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, String, CompareOptions) Searches for the specified substring and returns the zero-based index of the first occurrence within the entire source string using the specified CompareOptions value.
Public method IndexOf(String, String, Int32) Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, Char, Int32, CompareOptions) Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, Char, Int32, Int32) Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, String, Int32, CompareOptions) Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, String, Int32, Int32) Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, Char, Int32, Int32, CompareOptions) Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library IndexOf(String, String, Int32, Int32, CompareOptions) Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library IsPrefix(String, String) Determines whether the specified source string starts with the specified prefix.
Public method Supported by the XNA Framework Supported by Portable Class Library IsPrefix(String, String, CompareOptions) Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.
Public method Static member IsSortable(Char) Indicates whether a specified Unicode character is sortable.
Public method Static member IsSortable(String) Indicates whether a specified Unicode string is sortable.
Public method Supported by the XNA Framework Supported by Portable Class Library IsSuffix(String, String) Determines whether the specified source string ends with the specified suffix.
Public method Supported by the XNA Framework Supported by Portable Class Library IsSuffix(String, String, CompareOptions) Determines whether the specified source string ends with the specified suffix using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, Char) Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, String) Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, Char, CompareOptions) Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string using the specified CompareOptions value.
Public method LastIndexOf(String, Char, Int32) Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, String, CompareOptions) Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string using the specified CompareOptions value.
Public method LastIndexOf(String, String, Int32) Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, Char, Int32, CompareOptions) Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, Char, Int32, Int32) Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, String, Int32, CompareOptions) Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, String, Int32, Int32) Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, Char, Int32, Int32, CompareOptions) Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index using the specified CompareOptions value.
Public method Supported by the XNA Framework Supported by Portable Class Library LastIndexOf(String, String, Int32, Int32, CompareOptions) Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index using the specified CompareOptions value.
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current CompareInfo. (Overrides Object.ToString().)
Top
  Name Description
Explicit interface implemetation Private method IDeserializationCallback.OnDeserialization Runs when the entire object graph has been deserialized.
Top

The CultureInfo class includes a CompareInfo property that is an instance of this class. String.Compare uses the information in CultureInfo.CompareInfo to compare strings.

CompareInfo provides the GetCompareInfo method, instead of public constructors, to allow for late-bound access.

To create a CompareInfo object for any culture, the application should use the CultureInfo.CompareInfo property, or use the GetCompareInfo method.

Ignored Search Values

Comparison operations, such as those performed by the IndexOf or LastIndexOf methods, can yield unexpected results if the value to search for is ignored. The search value is ignored if it is an empty string (""), a character or string consisting of characters having code points that are not considered in the operation because of comparison options, or a value with code points that have no linguistic significance. If the search value for the IndexOf method is an empty string, for example, the return value is zero.

Security Considerations

If a security decision depends on a string comparison or a case change, the application should use the InvariantCulture to ensure that the behavior is consistent, regardless of the culture settings of the operating system.

Note   When possible, the application should use string comparison methods that accept a CompareOptions value to specify the kind of comparison expected. As a general rule, user-facing comparisons are best served by the use of linguistic options (using the current culture), while security comparisons should specify Ordinal or OrdinalIgnoreCase.

The following code example shows how the CompareInfo object associated with a CultureInfo object affects string comparison.


using System;
using System.Text;
using System.Globalization;

public sealed class App
{
    static void Main(string[] args)
    {
        String[] sign = new String[] { "<", "=", ">" };

        // The code below demonstrates how strings compare 
        // differently for different cultures.
        String s1 = "Coté", s2 = "coté", s3 = "côte";

        // Set sort order of strings for French in France.
        CompareInfo ci = new CultureInfo("fr-FR").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s1, s2, sign[ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1]);

        // Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3, CompareOptions.None) + 1]);

        // Set sort order of strings for Japanese as spoken in Japan.
        ci = new CultureInfo("ja-JP").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using ja-JP Compare of coté < côte. 
        Console.WriteLine("ja-JP Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3) + 1]);
    }
}

// This code produces the following output.
// 
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

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.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ