Export (0) Print
Expand All

String Equality Operator

Determines whether two specified String objects have the same value.

[Visual Basic]
returnValue = String.op_Equality(a, b)
[C#]
public static bool operator ==(
 string a,
 string b
);
[C++]
public: static bool op_Equality(
 String* a,
 String* b
);
[JScript]
returnValue = a == b;

[Visual Basic] In Visual Basic, you can use the operators defined by a type, but you cannot define your own. You can use the Equals method instead of the String equality operator.

[JScript] In JScript, you can use the operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

a
A String or a null reference (Nothing in Visual Basic).
b
A String or a null reference (Nothing in Visual Basic).

Parameters [C#, C++]

a
A String or a null reference (Nothing in Visual Basic).
b
A String or a null reference (Nothing in Visual Basic).

Return Value

true if the value of a is the same as the value of b; otherwise, false.

Remarks

This operator is implemented using the Equals method, which means the comparands are tested for a combination of reference and value equality. The comparison is case-sensitive.

Example

[Visual Basic, C#, C++] The following example demonstrates the equality operator.

[Visual Basic] 
' Example for the String Equality operator.
Imports System
Imports Microsoft.VisualBasic

Module EqualityOp
       
    Sub Main()
        Console.WriteLine( _
            "This example of the String Equality operator " & vbCrLf & _
            "generates the following output." & vbCrLf)

        CompareAndDisplay("ijkl")
        CompareAndDisplay("ABCD")
        CompareAndDisplay("abcd")
    End Sub 'Main
       
    Sub CompareAndDisplay(Comparand As String)
        Dim Lower As String = "abcd"
          
        Console.WriteLine( _
            """{0}"" = ""{1}"" ?  {2}", _
            Lower, Comparand, Lower = Comparand)

    End Sub 'CompareAndDisplay
End Module 'EqualityOp

' This example of the String Equality operator 
' generates the following output.
'
' "abcd" = "ijkl" ?  False
' "abcd" = "ABCD" ?  False
' "abcd" = "abcd" ?  True

[C#] 
// Example for the String Equality operator.
using System;

class EqualityOp 
{
    public static void Main() 
    {
        Console.WriteLine( 
            "This example of the String Equality operator\n" +
            "generates the following output.\n" );

        CompareAndDisplay( "ijkl" );
        CompareAndDisplay( "ABCD" );
        CompareAndDisplay( "abcd" );
    }

    static void CompareAndDisplay( string Comparand )
    {
        String  Lower = "abcd";

        Console.WriteLine( 
            "\"{0}\" == \"{1}\" ?  {2}",
            Lower, Comparand, Lower == Comparand );
    }
}

/*
This example of the String Equality operator 
generates the following output.

"abcd" == "ijkl" ?  False
"abcd" == "ABCD" ?  False
"abcd" == "abcd" ?  True
*/

[C++] 
// Example for the String Equality operator.
#using <mscorlib.dll>
using namespace System;

void CompareAndDisplay( String* Comparand )
{
    String*  Lower = S"abcd";

    Console::WriteLine( 
        S"\"{0}\" == \"{1}\" ?  {2}",
        Lower, Comparand, __box( Lower == Comparand ) );
}

void main( ) 
{
    Console::WriteLine( 
        S"This example of the String Equality operator\n" 
        S"generates the following output.\n" );

    CompareAndDisplay( S"ijkl" );
    CompareAndDisplay( S"ABCD" );
    CompareAndDisplay( S"abcd" );
}

/*
This example of the String Equality operator 
generates the following output.

"abcd" == "ijkl" ?  False
"abcd" == "ABCD" ?  False
"abcd" == "abcd" ?  True
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

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, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace

Show:
© 2014 Microsoft