CompareInfo CompareInfo CompareInfo CompareInfo Class

Definition

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

public ref class CompareInfo : System::Runtime::Serialization::IDeserializationCallback
[System.Runtime.InteropServices.ComVisible(true)]
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
type CompareInfo = class
    interface IDeserializationCallback
Public Class CompareInfo
Implements IDeserializationCallback
Inheritance
CompareInfoCompareInfoCompareInfoCompareInfo
Attributes
Implements

Examples

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

using namespace System;
using namespace System::Text;
using namespace System::Globalization;

int main()
{
    array<String^>^ sign = gcnew array<String^> { "<", "=", ">" };

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

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

    // Display the result using fr-FR Compare of Coté = coté.
    Console::WriteLine(L"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(L"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 = (gcnew CultureInfo("ja-JP"))->CompareInfo;
    Console::WriteLine(L"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
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
Imports System
Imports System.Text
Imports System.Globalization

NotInheritable Public Class App
    Shared Sub Main(ByVal args() As String) 
        Dim sign() As String = {"<", "=", ">"}
        
        ' The code below demonstrates how strings compare 
        ' differently for different cultures.
        Dim s1 As String = "Coté"
        Dim s2 As String = "coté"
        Dim s3 As String = "côte"
        
        ' Set sort order of strings for French in France.
        Dim ci As CompareInfo = 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)))
    End Sub 'Main
End Class 'App

' 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

Remarks

Conventions for comparing and sorting data vary from culture to culture. For example, sort order may be based on phonetics or on the visual representation of characters. In East Asian languages, characters are sorted by the stroke and radical of ideographs. Sorting also depends on the order languages and cultures use for the alphabet. For example, the Danish language has an "Æ" character that it sorts after "Z" in the alphabet. In addition, comparisons may be case-sensitive or case-insensitive, and casing rules may also differ by culture. The CompareInfo class is responsible for maintaining this culture-sensitive string comparison data and for performing culture-sensitive string operations.

Typically, you do not have to instantiate a CompareInfo object directly, because one is used implicitly by all non-ordinal string comparison operations, including calls to the String.Compare method. However, if you do want to retrieve a CompareInfo object, you can do it in one of these ways:

Ignored search values

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. Comparison methods such as IndexOf and LastIndexOf do not consider such characters when they perform a culture-sensitive comparison. Ignorable characters include:

  • String.Empty. Culture-sensitive comparison methods will always find an empty string at the beginning (index zero) of the string being searched.

  • A character or string consisting of characters with code points that are not considered in the operation because of comparison options, In particular, the CompareOptions.IgnoreNonSpace and CompareOptions.IgnoreSymbols options produce searches in which symbols and nonspacing combining characters are ignored.

  • A string with code points that have no linguistic significance. For example, a soft hyphen (U+00AD) is always ignored in a culture-sensitive string comparison.

Security considerations

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

Note

When possible, you should use string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify Ordinal or OrdinalIgnoreCase for security comparisons.

Properties

LCID LCID LCID LCID

Gets the properly formed culture identifier for the current CompareInfo.

Name Name Name Name

Gets the name of the culture used for sorting operations by this CompareInfo object.

Version Version Version Version

Gets information about the version of Unicode used for comparing and sorting strings.

Methods

Compare(String, Int32, Int32, String, Int32, Int32) Compare(String, Int32, Int32, String, Int32, Int32) Compare(String, Int32, Int32, String, Int32, Int32) Compare(String, Int32, Int32, String, Int32, Int32)

Compares a section of one string with a section of another string.

Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) 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.

Compare(String, Int32, String, Int32) Compare(String, Int32, String, Int32) Compare(String, Int32, String, Int32) Compare(String, Int32, String, Int32)

Compares the end section of a string with the end section of another string.

Compare(String, Int32, String, Int32, CompareOptions) Compare(String, Int32, String, Int32, CompareOptions) Compare(String, Int32, String, Int32, CompareOptions) 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.

Compare(String, String) Compare(String, String) Compare(String, String) Compare(String, String)

Compares two strings.

Compare(String, String, CompareOptions) Compare(String, String, CompareOptions) Compare(String, String, CompareOptions) Compare(String, String, CompareOptions)

Compares two strings using the specified CompareOptions value.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

GetCompareInfo(Int32) GetCompareInfo(Int32) GetCompareInfo(Int32) GetCompareInfo(Int32)

Initializes a new CompareInfo object that is associated with the culture with the specified identifier.

GetCompareInfo(Int32, Assembly) GetCompareInfo(Int32, Assembly) GetCompareInfo(Int32, Assembly) 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.

GetCompareInfo(String) GetCompareInfo(String) GetCompareInfo(String) GetCompareInfo(String)

Initializes a new CompareInfo object that is associated with the culture with the specified name.

GetCompareInfo(String, Assembly) GetCompareInfo(String, Assembly) GetCompareInfo(String, Assembly) 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.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as a hash function for the current CompareInfo for hashing algorithms and data structures, such as a hash table.

GetHashCode(String, CompareOptions) GetHashCode(String, CompareOptions) GetHashCode(String, CompareOptions) GetHashCode(String, CompareOptions)

Gets the hash code for a string based on specified comparison options.

GetSortKey(String) GetSortKey(String) GetSortKey(String) GetSortKey(String)

Gets the sort key for the specified string.

GetSortKey(String, CompareOptions) GetSortKey(String, CompareOptions) GetSortKey(String, CompareOptions) GetSortKey(String, CompareOptions)

Gets a SortKey object for the specified string using the specified CompareOptions value.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(String, Char) IndexOf(String, Char) IndexOf(String, Char) IndexOf(String, Char)

Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string.

IndexOf(String, Char, CompareOptions) IndexOf(String, Char, CompareOptions) IndexOf(String, Char, CompareOptions) 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.

IndexOf(String, Char, Int32) IndexOf(String, Char, Int32) IndexOf(String, Char, Int32) 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.

IndexOf(String, Char, Int32, CompareOptions) IndexOf(String, Char, Int32, CompareOptions) IndexOf(String, Char, Int32, CompareOptions) 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.

IndexOf(String, Char, Int32, Int32) IndexOf(String, Char, Int32, Int32) IndexOf(String, Char, Int32, Int32) 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.

IndexOf(String, Char, Int32, Int32, CompareOptions) IndexOf(String, Char, Int32, Int32, CompareOptions) IndexOf(String, Char, Int32, Int32, CompareOptions) 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.

IndexOf(String, String) IndexOf(String, String) IndexOf(String, String) IndexOf(String, String)

Searches for the specified substring and returns the zero-based index of the first occurrence within the entire source string.

IndexOf(String, String, CompareOptions) IndexOf(String, String, CompareOptions) IndexOf(String, String, CompareOptions) 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.

IndexOf(String, String, Int32) IndexOf(String, String, Int32) IndexOf(String, String, Int32) 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.

IndexOf(String, String, Int32, CompareOptions) IndexOf(String, String, Int32, CompareOptions) IndexOf(String, String, Int32, CompareOptions) 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.

IndexOf(String, String, Int32, Int32) IndexOf(String, String, Int32, Int32) IndexOf(String, String, Int32, Int32) 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.

IndexOf(String, String, Int32, Int32, CompareOptions) IndexOf(String, String, Int32, Int32, CompareOptions) IndexOf(String, String, Int32, Int32, CompareOptions) 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.

IsPrefix(String, String) IsPrefix(String, String) IsPrefix(String, String) IsPrefix(String, String)

Determines whether the specified source string starts with the specified prefix.

IsPrefix(String, String, CompareOptions) IsPrefix(String, String, CompareOptions) IsPrefix(String, String, CompareOptions) IsPrefix(String, String, CompareOptions)

Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.

IsSortable(Char) IsSortable(Char) IsSortable(Char) IsSortable(Char)

Indicates whether a specified Unicode character is sortable.

IsSortable(String) IsSortable(String) IsSortable(String) IsSortable(String)

Indicates whether a specified Unicode string is sortable.

IsSuffix(String, String) IsSuffix(String, String) IsSuffix(String, String) IsSuffix(String, String)

Determines whether the specified source string ends with the specified suffix.

IsSuffix(String, String, CompareOptions) IsSuffix(String, String, CompareOptions) IsSuffix(String, String, CompareOptions) IsSuffix(String, String, CompareOptions)

Determines whether the specified source string ends with the specified suffix using the specified CompareOptions value.

LastIndexOf(String, Char) LastIndexOf(String, Char) LastIndexOf(String, Char) LastIndexOf(String, Char)

Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string.

LastIndexOf(String, Char, CompareOptions) LastIndexOf(String, Char, CompareOptions) LastIndexOf(String, Char, CompareOptions) 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.

LastIndexOf(String, Char, Int32) LastIndexOf(String, Char, Int32) LastIndexOf(String, Char, Int32) 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.

LastIndexOf(String, Char, Int32, CompareOptions) LastIndexOf(String, Char, Int32, CompareOptions) LastIndexOf(String, Char, Int32, CompareOptions) 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.

LastIndexOf(String, Char, Int32, Int32) LastIndexOf(String, Char, Int32, Int32) LastIndexOf(String, Char, Int32, Int32) 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.

LastIndexOf(String, Char, Int32, Int32, CompareOptions) LastIndexOf(String, Char, Int32, Int32, CompareOptions) LastIndexOf(String, Char, Int32, Int32, CompareOptions) 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.

LastIndexOf(String, String) LastIndexOf(String, String) LastIndexOf(String, String) LastIndexOf(String, String)

Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string.

LastIndexOf(String, String, CompareOptions) LastIndexOf(String, String, CompareOptions) LastIndexOf(String, String, CompareOptions) 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.

LastIndexOf(String, String, Int32) LastIndexOf(String, String, Int32) LastIndexOf(String, String, Int32) 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.

LastIndexOf(String, String, Int32, CompareOptions) LastIndexOf(String, String, Int32, CompareOptions) LastIndexOf(String, String, Int32, CompareOptions) 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.

LastIndexOf(String, String, Int32, Int32) LastIndexOf(String, String, Int32, Int32) LastIndexOf(String, String, Int32, Int32) 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.

LastIndexOf(String, String, Int32, Int32, CompareOptions) LastIndexOf(String, String, Int32, Int32, CompareOptions) LastIndexOf(String, String, Int32, Int32, CompareOptions) 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.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current CompareInfo object.

Explicit Interface Implementations

IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object)

Runs when the entire object graph has been deserialized.

Extension Methods

GetStringComparer(CompareInfo, CompareOptions) GetStringComparer(CompareInfo, CompareOptions) GetStringComparer(CompareInfo, CompareOptions) GetStringComparer(CompareInfo, CompareOptions)

Returns a StringComparer object based on the culture-sensitive string comparison rules of a specified CompareInfo object.

Applies to