Windows Dev Center

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 (String^)


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

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

virtual int CompareTo(
	String^ strB
) sealed


Type: System::String^

The string to compare with this instance.

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 strB parameter.



Less than zero

This instance precedes strB.


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

Greater than zero

This instance follows strB.


strB is null.

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.


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.

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

This method implements the System::IComparable<T> interface and performs slightly better than the String::CompareTo(Object^) method, because it does not have to determine whether the strB argument is a mutable value type that must be boxed, and it does not have to cast its parameter from an Objectto aString.

Notes to Callers:

Character sets include ignorable characters. The CompareTo(String^) 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 the CompareOrdinal(String^, String^) method.

The following example uses the CompareTo method to compare the current string instance with another string.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()

   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   String^ xmlData = "<book xmlns:bk='urn:samples'></book>";
   doc->Load( gcnew StringReader( xmlData ) );

   // Create a new element and add it to the document.
   XmlElement^ elem = doc->CreateElement( "bk", "genre", "urn:samples" );
   elem->InnerText = "fantasy";
   doc->DocumentElement->AppendChild( elem );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );

The following example demonstrates generic and non-generic versions of the CompareTo method for several value and reference types.

   void PrintNamespacePairs( XmlSerializerNamespaces^ namespaces )
      array<XmlQualifiedName^>^ qualifiedNames = namespaces->ToArray();
      for ( int i = 0; i < qualifiedNames->Length; i++ )
         Console::WriteLine( "{0}\t{1}",
            qualifiedNames[ i ]->Name, qualifiedNames[ i ]->Namespace );

Universal Windows Platform
Available since 4.5
.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
© 2015 Microsoft