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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Compares the end section of a string with the end section of another string using the specified CompareOptions value.

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

Public Overridable Function Compare (
	string1 As String,
	offset1 As Integer,
	string2 As String,
	offset2 As Integer,
	options As CompareOptions
) As Integer


Type: System.String

The first string to compare.

Type: System.Int32

The zero-based index of the character in string1 at which to start comparing.

Type: System.String

The second string to compare.

Type: System.Int32

The zero-based index of the character in string2 at which to start comparing.

Type: System.Globalization.CompareOptions

A value that defines how string1 and string2 should be compared. options is either the enumeration value Ordinal, or a bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, IgnoreKanaType, and StringSort.

Return Value

Type: System.Int32

A 32-bit signed integer indicating the lexical relationship between the two comparands.




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.

Exception Condition

offset1 or offset2 is less than zero.


offset1 is greater than or equal to the number of characters in string1.


offset2 is greater than or equal to the number of characters in string2.


options contains an invalid CompareOptions value.

If a security decision depends on a string comparison or a case change, you should use the InvariantCulture property to ensure that the behavior is consistent regardless of the culture settings of the operating system.


When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify Ordinal or OrdinalIgnoreCase for security comparisons.

Notes to Callers:

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. The Compare(String, Int32, String, Int32, CompareOptions) method does not consider such characters when performing a 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 compares portions of two strings using different CompareOptions settings.

Imports System
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' 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.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(10), myStr2.Substring(10))
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 10, myStr2, 10))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub 'Main 

End Class 'SamplesCompareInfo

'This code produces the following output.
'Comparing "ill's clients" and "ills clients"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -76
'   With StringSort                   : -1
'   With IgnoreCase                   : 1
'   With IgnoreSymbols                : 0
'   With IgnoreCase and IgnoreSymbols : 0

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top