Export (0) Print
Expand All

String.CompareTo Method (String)

.NET Framework 1.1

Compares this instance with a specified String object.

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

Parameters

strB
A String.

Return Value

A 32-bit signed integer indicating the lexical relationship between the two comparands.

Value Condition
Less than zero This instance is less than strB.
Zero This instance is equal to strB.
Greater than zero This instance is greater than strB.

-or-

strB is a null reference (Nothing in Visual Basic).

Remarks

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 Compare method.

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();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

String Class | String Members | System Namespace | String.CompareTo Overload List | Compare | CompareOrdinal

Show:
© 2014 Microsoft