Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.CompareTo Method (Object)

 
System_CAPS_noteNote

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

Compares this instance with a specified Object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.

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

Public Function CompareTo (
	value As Object
) As Integer

Parameters

value
Type: System.Object

An object that evaluates to a String.

Return Value

Type: System.Int32

A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the value parameter.

Value

Condition

Less than zero

This instance precedes value.

Zero

This instance has the same position in the sort order as value.

Greater than zero

This instance follows value.

-or-

value is null.

Exception Condition
ArgumentException

value is not a String.

value must be a String object.

System_CAPS_cautionCaution

The CompareTo method was 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 strings are equivalent. To determine whether two strings are equivalent, call the Equals method.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

For more information about the behavior of this method, see the Remarks section of the String.Compare(String, String) method.

Notes to Callers:

Character sets include ignorable characters. TheCompareTo method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.

Module Example
   Public Sub Main()
      Dim s1 As String = "ani" + ChrW(&h00AD) + "mal"
      Dim o1 As Object = "animal"

      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, o1, s1.CompareTo(o1))
  End Sub
End Module
' The example displays the following output:
'       Comparison of 'ani-mal' and 'animal': 0

To recognize ignorable characters in a string comparison, call theCompareOrdinal(String, String) method.

The following example uses the CompareTo method with an Object. Because it attempts to compare a String instance to a TestClass object, the method throws an ArgumentException.

Public Class TestClass
End Class 


Public Class Example
   Public Shared Sub Main()
      Dim test As New TestClass()
      Dim objectsToCompare() As Object = { test, test.ToString(), 123,
                                           123.ToString(), "some text",
                                           "Some Text" }
      Dim s As String = "some text"
      For Each objectToCompare In objectsToCompare
         Try
            Dim i As Integer = s.CompareTo(objectToCompare)
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i)
         Catch e As ArgumentException
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name)
         End Try
      Next
   End Sub 
End Class 
' The example displays the following output:
'       Bad argument: TestClass (type TestClass)
'       Comparing 'some text' with 'TestClass': -1
'       Bad argument: 123 (type Int32)
'       Comparing 'some text' with '123': 1
'       Comparing 'some text' with 'some text': 0
'       Comparing 'some text' with 'Some Text': -1

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2017 Microsoft