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

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

Compares substrings of two specified String objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two substrings to one another in the sort order.

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

Syntax

'Declaration
Public Shared Function Compare ( _
    strA As String, _
    indexA As Integer, _
    strB As String, _
    indexB As Integer, _
    length As Integer, _
    culture As CultureInfo, _
    options As CompareOptions _
) As Integer
public static int Compare(
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length,
    CultureInfo culture,
    CompareOptions options
)

Parameters

  • indexA
    Type: System.Int32
    The starting position of the substring within strA.
  • indexB
    Type: System.Int32
    The starting position of the substring within strB.
  • length
    Type: System.Int32
    The maximum number of characters in the substrings to compare.

Return Value

Type: System.Int32
An integer that indicates the lexical relationship between the two substrings.

Value

Condition

Less than zero

The substring in strA is less than the substring in strB.

Zero

The substrings are equal or length is zero.

Greater than zero

The substring in strA is greater than the substring in strB.

Exceptions

Exception Condition
ArgumentException

options is not a CompareOptions value.

ArgumentOutOfRangeException

indexA is greater than strA.Length.

-or-

indexB is greater than strB.Length.

-or-

indexA, indexB, or length is negative.

-or-

Either strA or strB is nulla null reference (Nothing in Visual Basic), and length is greater than zero.

ArgumentNullException

culture is nulla null reference (Nothing in Visual Basic).

Remarks

The substrings to compare start in strA at position indexA, and in strB at position indexB. The length of the first substring is the length of strA minus indexA. The length of the second substring is the length of strB minus indexB.

The number of characters to compare is the lesser of the lengths of the two substrings, and length. The indexA, indexB, and length parameters must be nonnegative.

The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetic order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.

Caution noteCaution:

The Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method is designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two substrings are equivalent (that is, when the purpose of the method call is to test for a return value of zero). To determine whether two strings are equivalent, call the Equals method.

One or both of strA and strB can be nulla null reference (Nothing in Visual Basic). By definition, any string, including String.Empty, compares greater than a null reference, and two null references compare equal to each other.

The comparison can be further specified by the options parameter, which consists of one or more members of the CompareOptions enumeration. However, because the purpose of this method is to conduct a culture-sensitive string comparison, the CompareOptions.Ordinal and CompareOptions.OrdinalIgnoreCase values have no effect.

The comparison terminates when an inequality is discovered or both substrings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, the string with the remaining characters is considered greater. The return value is the result of the last comparison performed.

Examples

The following example uses the Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method to compare the last names of two people. It then lists them in alphabetical order.

Imports System.Globalization

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim name1 As String = "Jack Smith"
      Dim name2 = "John Doe"

      ' Get position of space character.
      Dim index1 As Integer = name1.IndexOf(" ")
      index1 = CInt(IIf(index1 < 0, 0, index1 - 1))

      Dim index2 As Integer = name2.IndexOf(" ")
      index1 = CInt(IIf(index1 < 0, 0, index1 - 1))

      Dim length As Integer = Math.Max(name1.Length, name2.Length)

      outputBlock.Text += "Sorted alphabetically by last name:" & vbCrLf
      If String.Compare(name1, index1, name2, index2, length, _
                        New CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0 Then
         outputBlock.Text += name1 + vbCrLf + name2
      Else
         outputBlock.Text += name2 + vbCrLf + name1
      End If
   End Sub
End Module
public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string name1 = "Jack Smith";
      string name2 = "John Doe";

      // Get position of space character.
      int index1 = name1.IndexOf(" ");
      index1 = index1 < 0 ? 0 : index1--;

      int index2 = name2.IndexOf(" ");
      index1 = index1 < 0 ? 0 : index1--;

      int length = Math.Max(name1.Length, name2.Length);

      outputBlock.Text += "Sorted alphabetically by last name:\n";
      if (String.Compare(name1, index1, name2, index2, length, 
                         new CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0)
         outputBlock.Text += name1 + "\n" + name2; 
      else
         outputBlock.Text += name2 + "\n" + name1; 
   }
}

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.