String.Compare 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)

public static int Compare(
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length,
	CultureInfo culture,
	CompareOptions options


Type: System.String

The first string to use in the comparison.

Type: System.Int32

The starting position of the substring within strA.

Type: System.String

The second string to use in the comparison.

Type: System.Int32

The starting position of the substring within strB.

Type: System.Int32

The maximum number of characters in the substrings to compare.

Type: System.Globalization.CultureInfo

An object that supplies culture-specific comparison information.

Type: System.Globalization.CompareOptions

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

Return Value

Type: System.Int32

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



Less than zero

The substring in strA precedes the substring in strB in the sort order.


The substrings occur in the same position in the sort order, or length is zero.

Greater than zero

The substring in strA follows the substring in strB in the sort order.

Exception Condition

options is not a CompareOptions value.


indexA is greater than strA.Length.


indexB is greater than strB.Length.


indexA, indexB, or length is negative.


Either strA or strB is null, and length is greater than zero.


culture is null.

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.


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 null. 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 System.Globalization.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.

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 CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for the options parameter.

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.

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); 
         Console.WriteLine("{0}\n{1}", name2, name1); 
// The example displays the following output:
//       Sorted alphabetically by last name:
//       John Doe
//       Jack Smith

.NET Framework
Available since 2.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top