共用方式為


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

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

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.

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

Syntax

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

Parameters

  • offset1
    Type: System.Int32
    The zero-based index of the character in string1 at which to start comparing.
  • offset2
    Type: System.Int32
    The zero-based index of the character in string2 at which to start comparing.

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 is less than the specified section of string2.

greater than zero

The specified section of string1 is greater than the specified section of string2.

Exceptions

Exception Condition
ArgumentOutOfRangeException

offset1 or offset2 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.

Remarks

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.

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), string2.Substring(1)) & vbCrLf
      outputBlock.Text += String.Format("   With fr-FR Culture: {0}", compareFrFR.Compare(string1, 1, string2, 1)) & vbCrLf
      outputBlock.Text += String.Format("   With en-US Culture: {0}", compareEnUS.Compare(string1, 1, string2, 1)) & vbCrLf
      outputBlock.Text += String.Format("   With Invariant Culture: {0}", compareInv.Compare(string1, 1, string2, 1)) & 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), string2.Substring(1));
      outputBlock.Text += String.Format("   With fr-FR Culture: {0}\n", compareFrFR.Compare(string1, 1, string2, 1));
      outputBlock.Text += String.Format("   With en-US Culture: {0}\n", compareEnUS.Compare(string1, 1, string2, 1));
      outputBlock.Text += String.Format("   With Invariant Culture: {0}\n", compareInv.Compare(string1, 1, string2, 1));
   }
}
/*
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.