Skip to main content
.NET Framework Class Library
StringCompare Method (String, Int32, String, Int32, Int32, CultureInfo, CompareOptions)

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 each other in the sort order.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Compare ( _
	strA As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_0%], _
	indexA As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_1%], _
	strB As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_2%], _
	indexB As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_3%], _
	length As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_4%], _
	culture As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_5%], _
	options As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_6%] _
) As [%$TOPIC/cc190416_en-us_VS_110_2_0_0_0_7%]
public static [%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_0%] Compare(
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_1%] strA,
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_2%] indexA,
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_3%] strB,
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_4%] indexB,
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_5%] length,
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_6%] culture,
	[%$TOPIC/cc190416_en-us_VS_110_2_0_1_0_7%] options
)
public:
static [%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_0%] Compare(
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_1%]^ strA, 
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_2%] indexA, 
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_3%]^ strB, 
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_4%] indexB, 
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_5%] length, 
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_6%]^ culture, 
	[%$TOPIC/cc190416_en-us_VS_110_2_0_2_0_7%] options
)
static member Compare : 
        strA:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_0%] * 
        indexA:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_1%] * 
        strB:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_2%] * 
        indexB:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_3%] * 
        length:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_4%] * 
        culture:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_5%] * 
        options:[%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_6%] -> [%$TOPIC/cc190416_en-us_VS_110_2_0_3_0_7%]

Parameters

strA
Type: SystemString

The first string to use in the comparison.

indexA
Type: SystemInt32

The starting position of the substring within strA.

strB
Type: SystemString

The second string to use in the comparison.

indexB
Type: SystemInt32

The starting position of the substring within strB.

length
Type: SystemInt32

The maximum number of characters in the substrings to compare.

culture
Type: System.GlobalizationCultureInfo

An object that supplies culture-specific comparison information.

options
Type: System.GlobalizationCompareOptions

Options to use when performing the comparison (such as ignoring case or symbols).

Return Value

Type: SystemInt32
An integer that indicates the lexical relationship between the two substrings, as shown in the following table.

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
ExceptionCondition
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 , and length is greater than zero.

ArgumentNullException

culture is .

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 alphabetical 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 . By definition, any string, including StringEmpty, 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 System.GlobalizationCompareOptions enumeration. However, because the purpose of this method is to conduct a culture-sensitive string comparison, the CompareOptionsOrdinal and CompareOptionsOrdinalIgnoreCase 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.

Notes to Callers

Character sets include ignorable characters. The Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method does not consider these characters when it performs a linguistic or culture-sensitive comparison. To recognize ignorable characters in your comparison, supply a value of CompareOptionsOrdinal or CompareOptionsOrdinalIgnoreCase for the options parameter.

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 Main()
      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)

      Console.WriteLIne("Sorted alphabetically by last name:")
      If String.Compare(name1, index1, name2, index2, length, _
                        New CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0 Then
         Console.WriteLine("{0}{1}{2}", name1, vbCrLf, name2)
      Else
         Console.WriteLine("{0}{1}{2}", name2, vbCrLf, name1)
      End If 
   End Sub 
End Module 
' The example displays the following output; 
'       Sorted alphabetically by last name: 
'       John Doe 
'       Jack Smith
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      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);

      Console.WriteLine("Sorted alphabetically by last name:");
      if (String.Compare(name1, index1, name2, index2, length, 
                         new CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0)
         Console.WriteLine("{0}\n{1}", name1, name2); 
      else
         Console.WriteLine("{0}\n{1}", name2, name1); 
   }
}
// The example displays the following output: 
//       Sorted alphabetically by last name: 
//       John Doe 
//       Jack Smith
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5 SP1, 3.0 SP2, 2.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.