Freigeben über


CompareInfo Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

System.Object
  System.Globalization.CompareInfo

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Class CompareInfo
[ComVisibleAttribute(true)]
public class CompareInfo

The CompareInfo type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Name Gets the name of the culture used for sorting operations by this CompareInfo object.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare(String, String) Compares two strings and returns an integer that indicates their relationship to one another in the sort order.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare(String, String, CompareOptions) Compares two strings using the specified CompareOptions value and returns an integer that indicates their relationship to one another in the sort order.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare(String, Int32, String, Int32) Compares the end section of a string with the end section of another string and returns an integer that indicates their relationship to one another in the sort order.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 and returns an integer that indicates their relationship to one another in the sort order.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare(String, Int32, Int32, String, Int32, Int32) Compares a section of one string with a section of another string and returns an integer that indicates their relationship to one another in the sort order.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 and returns an integer that indicates their relationship to one another in the sort order.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals Determines whether the specified object is equal to the current CompareInfo object. (Overrides Object.Equals(Object).)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCompareInfo Initializes a new CompareInfo object that is associated with the culture with the specified name.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IndexOf(String, Char) Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IndexOf(String, String) Searches for the specified substring and returns the zero-based index of the first occurrence within the entire source string.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsPrefix(String, String) Determines whether the specified source string starts with the specified prefix.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsPrefix(String, String, CompareOptions) Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSuffix(String, String) Determines whether the specified source string ends with the specified suffix.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSuffix(String, String, CompareOptions) Determines whether the specified source string ends with the specified suffix using the specified CompareOptions value.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 LastIndexOf(String, Char) Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 LastIndexOf(String, String) Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current CompareInfo. (Overrides Object.ToString().)

Top

Remarks

The CultureInfo class includes a CompareInfo property that returns an instance of this class. Some string comparison methods, such as String.Compare, use the information in a CultureInfo.CompareInfo object to compare strings.

The CompareInfo class 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 CompareInfo.GetCompareInfo method and provide the name of the culture whose CompareInfo object is to be returned.

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 under the following conditions:

  • It is an empty string ("").

  • It consists of characters whose code points are not considered in the operation because of comparison options. For example, this occurs if the search string includes symbols and the value passed to a parameter of type CompareOptions includes the CompareOptions.IgnoreSymbols flag.

  • It consists of characters whose code points 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, the application should test for equality using ordinal comparison. It should not use the Compare method.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

String comparison operations for strings containing trigraphs may not work as expected for cultures where trigraphs are used, such as "dzh" in the culture hu-HU. For example, IndexOf(String, String, CompareOptions) and IsSuffix(String, String, CompareOptions) may return an incorrect result.

Examples

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

Imports System.Text
Imports System.Globalization

Public NotInheritable Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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

      ' Display the result using fr-FR Compare of Coté = coté.      
      outputBlock.Text += String.Format("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.
      outputBlock.Text += String.Format("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

      ' Display the result using ja-JP Compare of coté < côte. 
      outputBlock.Text += String.Format("ja-JP Compare: {0} {2} {1}", _
                        s2, s3, sign((ci.Compare(s2, s3) + 1)))
   End Sub 
End Class 

' This code produces the following output.
' 
' fr-FR Compare: Coté = coté
' fr-FR Compare: coté > côte
' ja-JP Compare: coté < côte
using System;
using System.Text;
using System.Globalization;

public sealed class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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;

      // Display the result using fr-FR Compare of Coté = coté.     
      outputBlock.Text += String.Format("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.
      outputBlock.Text += String.Format("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;

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

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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

See Also

Reference