Compartir a través de


CompareInfo.Compare Method (String, Int32, Int32, String, Int32, Int32)

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

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.

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

Syntax

'Declaration
Public Overridable Function Compare ( _
    string1 As String, _
    offset1 As Integer, _
    length1 As Integer, _
    string2 As String, _
    offset2 As Integer, _
    length2 As Integer _
) As Integer
public virtual int Compare(
    string string1,
    int offset1,
    int length1,
    string string2,
    int offset2,
    int length2
)

Parameters

  • offset1
    Type: System.Int32
    The zero-based index of the character in string1 at which to start the comparison.
  • length1
    Type: System.Int32
    The number of consecutive characters in string1 to compare.
  • offset2
    Type: System.Int32
    The zero-based index of the character in string2 at which to start the comparison.
  • length2
    Type: System.Int32
    The number of consecutive characters in string2 to compare.

Return Value

Type: System.Int32
An integer that indicates the relationship between the two strings in the sort order, as follows:

Value

Condition

zero

The two strings are equal.

less than zero

The specified section of string1 precedes the specified section of string2.

greater than zero

The specified section of string1 follows the specified section of string2.

Exceptions

Exception Condition
ArgumentOutOfRangeException

offset1 or length1 or offset2 or length2 is less than zero.

-or-

offset1 is greater than or equal to the number of characters in string1.

-or-

offset2 is greater than or equal to the number of characters in string2.

-or-

length1 is greater than the number of characters from offset1 to the end of string1.

-or-

length2 is greater than the number of characters from offset2 to the end of string2.

Remarks

If a security decision depends on a string comparison, the application should test for quality using ordinal comparison. It should not base a security decision on the Compare method.

Examples

The following example compares portions of two strings using the different CompareInfo objects:

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Define the strings to compare.
      Dim string1 As String = "coté"
      Dim string2 As String = "côte"

      ' Get CompareInfo objects for three cultures.
      Dim compareInv As CompareInfo = CultureInfo.InvariantCulture.CompareInfo
      Dim compareEnUS As CompareInfo = CompareInfo.GetCompareInfo("en-US")
      Dim compareFrFR As CompareInfo = CompareInfo.GetCompareinfo("fr-FR")

      ' Compare the two strings using each CompareInfo object.
      outputBlock.Text += String.Format("Comparing ""{0}"" and ""{1}""", string1.Substring(1, 3), string2.Substring(1, 3)) & vbCrLf
      outputBlock.Text += String.Format("   With fr-FR Culture: {0}", compareFrFR.Compare(string1, 1, 3, string2, 1, 3)) & vbCrLf
      outputBlock.Text += String.Format("   With en-US Culture: {0}", compareEnUS.Compare(string1, 1, 3, string2, 1, 3)) & vbCrLf
      outputBlock.Text += String.Format("   With Invariant Culture: {0}", compareInv.Compare(string1, 1, 3, string2, 1, 3)) & vbCrLf
   End Sub
End Class
' This example produces the following output:
'       Comparing "oté" and "ôte"
'          With fr-FR Culture: 1
'          With en-US Culture: -1
'          With Invariant Culture: -1
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Define the strings to compare.
      string string1 = "coté";
      string string2 = "côte";

      // Get CompareInfo objects for three cultures.
      CompareInfo compareInv = CultureInfo.InvariantCulture.CompareInfo;
      CompareInfo compareEnUS = CompareInfo.GetCompareInfo("en-US");
      CompareInfo compareFrFR = CompareInfo.GetCompareInfo("fr-FR");

      // Compare the two strings using each CompareInfo object.
      outputBlock.Text += String.Format("Comparing \"{0}\" and \"{1}\"\n", string1.Substring(1, 3), string2.Substring(1, 3));
      outputBlock.Text += String.Format("   With fr-FR Culture: {0}\n", compareFrFR.Compare(string1, 1, 3, string2, 1, 3));
      outputBlock.Text += String.Format("   With en-US Culture: {0}\n", compareEnUS.Compare(string1, 1, 3, string2, 1, 3));
      outputBlock.Text += String.Format("   With Invariant Culture: {0}\n", compareInv.Compare(string1, 1, 3, string2, 1, 3));
   }
}
/*
This example produces the following output:
       Comparing "oté" and "ôte"
          With fr-FR Culture: 1
          With en-US Culture: -1
          With Invariant Culture: -1
*/

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.