Windows Dev Center

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

Compares this instance with a specified object.

Overload List

Compares this instance with a specified Object.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function CompareTo(Object) As Integer Implements IComparable.CompareTo
[C#] public virtual int CompareTo(object);
[C++] public: virtual int CompareTo(Object*);
[JScript] public function CompareTo(Object) : int;

Compares this instance with a specified String object.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function CompareTo(String) As Integer
[C#] public int CompareTo(string);
[C++] public: int CompareTo(String*);
[JScript] public function CompareTo(String) : int;

Example

The following code example demonstrates how you can use the CompareTo method with another String.

[Visual Basic] 
Imports System


Public Class CompareToTest
    
    Public Shared Sub Main()
        Dim strFirst As String = "Goodbye"
        Dim strSecond As String = "Hello"
        Dim strThird As String = "a small string"
        Dim strFourth As String = "goodbye"
        
        
        ' compare a string to itself
        Console.WriteLine(CompareStrings(strFirst, strFirst))
        
        Console.WriteLine(CompareStrings(strFirst, strSecond))
        Console.WriteLine(CompareStrings(strFirst, strThird))
        
        ' compare a string to another string that varies only by case
        Console.WriteLine(CompareStrings(strFirst, strFourth))
        Console.WriteLine(CompareStrings(strFourth, strFirst))
    End Sub 'Main
    
    
    Private Shared Function CompareStrings(str1 As String, str2 As String) As String
        Dim cmpVal As Integer = str1.CompareTo(str2)
        If cmpVal = 0 Then
            
            ' compare the values, using the CompareTo method on the first string
            
            ' the values are the same
            Return "The strings have the same value!"
        Else
            If cmpVal > 0 Then
                
                ' the first value is greater than the second value
                Return "The first string is greater than the second string!"
            Else
                
                ' the second string is greater than the first string
                Return "The second string is greater than the first string!"
            End If
        End If
    End Function 'CompareStrings
End Class 'CompareToTest

[C#] 
using System;

public class CompareToTest {
    public static void Main() {

        string strFirst = "Goodbye";
        string strSecond = "Hello";
        string strThird = "a small string";
        string strFourth = "goodbye";

        // compare a string to itself
        Console.WriteLine (CompareStrings(strFirst, strFirst));

        Console.WriteLine (CompareStrings(strFirst, strSecond));
        Console.WriteLine (CompareStrings(strFirst, strThird));

        // compare a string to another string that varies only by case
        Console.WriteLine (CompareStrings(strFirst, strFourth));
        Console.WriteLine (CompareStrings(strFourth, strFirst));
    }

    private static string CompareStrings( string str1, string str2 ) {

        // compare the values, using the CompareTo method on the first string
        int cmpVal = str1.CompareTo(str2);

    if (cmpVal == 0) // the values are the same
            return "The strings have the same value!";

        else if (cmpVal > 0) // the first value is greater than the second value
            return "The first string is greater than the second string!";
            
        else // the second string is greater than the first string
            return "The second string is greater than the first string!";
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

String* CompareStrings(String* str1, String* str2)
{
   // compare the values, using the CompareTo method on the first string
   int cmpVal = str1->CompareTo(str2);

   if (cmpVal == 0) // the values are the same
      return S"The strings have the same value!";

   else if (cmpVal > 0) // the first value is greater than the second value
      return S"The first string is greater than the second string!";

   else // the second string is greater than the first string
      return S"The second string is greater than the first string!";
}

int main()
{
   String* strFirst = S"Goodbye";
   String* strSecond = S"Hello";
   String* strThird = S"a small String*";
   String* strFourth = S"goodbye";

   // compare a String* to itself
   Console::WriteLine (CompareStrings(strFirst, strFirst));

   Console::WriteLine (CompareStrings(strFirst, strSecond));
   Console::WriteLine (CompareStrings(strFirst, strThird));

   // compare a String* to another String* that varies only by case
   Console::WriteLine (CompareStrings(strFirst, strFourth));
   Console::WriteLine (CompareStrings(strFourth, strFirst));
}

[JScript] 
import System;

public class CompareToTest {
    public static function Main() : void  {

        var strFirst : String = "Goodbye";
        var strSecond : String = "Hello";
        var strThird : String = "a small string";
        var strFourth : String = "goodbye";

        // compare a string to itself
        Console.WriteLine (CompareStrings(strFirst, strFirst));

        Console.WriteLine (CompareStrings(strFirst, strSecond));
        Console.WriteLine (CompareStrings(strFirst, strThird));

        // compare a string to another string that varies only by case
        Console.WriteLine (CompareStrings(strFirst, strFourth));
        Console.WriteLine (CompareStrings(strFourth, strFirst));
    }

    private static function CompareStrings( str1 : String, str2 : String ) : String  {

        // compare the values, using the CompareTo method on the first string
        var cmpVal : int = str1.CompareTo(str2);

        if (cmpVal == 0) // the values are the same
            return "The strings have the same value!";

        else if (cmpVal > 0) // the first value is greater than the second value
            return "The first string is greater than the second string!";
            
        else // the second string is greater than the first string
            return "The second string is greater than the first string!";
    }
}
CompareToTest.Main();

See Also

String Class | String Members | System Namespace

Show:
© 2015 Microsoft