This documentation is archived and is not being maintained.

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

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
Dim instance As CompareInfo 
Dim string1 As String 
Dim offset1 As Integer 
Dim string2 As String 
Dim offset2 As Integer 
Dim options As CompareOptions 
Dim returnValue As Integer 

returnValue = instance.Compare(string1, _
	offset1, string2, offset2, options)


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

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

Return Value

Type: System.Int32




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.


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, the application should use the InvariantCulture to ensure that the behavior is consistent regardless of the culture settings of the operating system.

The following code 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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0