String::CompareTo Method (Object^)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on 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)

virtual int CompareTo(
	Object^ value
) sealed


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.



Less than zero

This instance precedes value.


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

Greater than zero

This instance follows value.


value is null.

Exception Condition

value is not a String.

value must be a String object.


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.

No code example is currently available or this language may not be supported.

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.

using namespace System;

public ref class TestClass{};

int main()
   TestClass^ test = gcnew TestClass;
   array<Object^>^ objectsToCompare = { test, test->ToString(), 123,
                                        (123).ToString(), "some text",
                                        "Some Text" };
   String^ s = "some text";
   for each (Object^ objectToCompare in objectsToCompare) {
      try {
         Int32 i = s->CompareTo(objectToCompare);
         Console::WriteLine("Comparing '{0}' with '{1}': {2}",
                            s, objectToCompare, i);
      catch (ArgumentException^ e) {
         Console::WriteLine("Bad argument: {0} (type {1})",
// 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
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top