共用方式為


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

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

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.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overridable Function Compare ( _
    string1 As String, _
    offset1 As Integer, _
    length1 As Integer, _
    string2 As String, _
    offset2 As Integer, _
    length2 As Integer, _
    options As CompareOptions _
) As Integer
[SecuritySafeCriticalAttribute]
public virtual int Compare(
    string string1,
    int offset1,
    int length1,
    string string2,
    int offset2,
    int length2,
    CompareOptions options
)

Parameters

  • offset1
    Type: System.Int32
    The zero-based index of the character in string1 at which to start comparing.
  • 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 comparing.
  • 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 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 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.

ArgumentException

options contains an invalid CompareOptions value.

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.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 The Compare method does not throw the expected exception, ArgumentOutOfRangeException, if you pass an invalid CompareOptions object.

Examples

The following example compares portions of two strings using different CompareOptions settings.

Imports System.Globalization

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      outputBlock.Text += String.Format("Comparing ""{0}"" and ""{1}""", myStr1.Substring(3, 10), myStr2.Substring(3, 10)) & vbCrLf
      outputBlock.Text += String.Format("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10)) & vbCrLf
      outputBlock.Text += String.Format("   With None                         : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None)) & vbCrLf
      outputBlock.Text += String.Format("   With Ordinal                      : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal)) & vbCrLf
      outputBlock.Text += String.Format("   With StringSort                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort)) & vbCrLf
      outputBlock.Text += String.Format("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase)) & vbCrLf
      outputBlock.Text += String.Format("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols)) & vbCrLf
      outputBlock.Text += String.Format("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols)) & vbCrLf

   End Sub 'Main 

End Class 'SamplesCompareInfo


'This code produces the following output.
'
'Comparing "Uncle Bill" and "uncle bill"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : 1
'   With IgnoreCase                   : 0
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0

using System;
using System.Globalization;

public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      outputBlock.Text += String.Format("Comparing \"{0}\" and \"{1}\"", myStr1.Substring(3, 10), myStr2.Substring(3, 10)) + "\n";
      outputBlock.Text += String.Format("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10)) + "\n";
      outputBlock.Text += String.Format("   With None                         : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None)) + "\n";
      outputBlock.Text += String.Format("   With Ordinal                      : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal)) + "\n";
      outputBlock.Text += String.Format("   With StringSort                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort)) + "\n";
      outputBlock.Text += String.Format("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase)) + "\n";
      outputBlock.Text += String.Format("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols)) + "\n";
      outputBlock.Text += String.Format("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols)) + "\n";

   }

}


/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/

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.