String.Equality(String, String) 運算子

定義

判斷兩個指定的字串是否具有相同的值。

public:
 static bool operator ==(System::String ^ a, System::String ^ b);
public static bool operator == (string a, string b);
public static bool operator == (string? a, string? b);
static member ( = ) : string * string -> bool
Public Shared Operator == (a As String, b As String) As Boolean

參數

a
String

要比較的第一個字串,或是 null

b
String

要比較的第二個字串,或 null

傳回

Boolean

如果 true 的值與 a 的值相同,則為 b,否則為 false

範例

下列範例示範等號比較運算子。

// Example for the String Equality operator.
using namespace System;
void CompareAndDisplay( String^ Comparand )
{
   String^ Lower = "abcd";
   Console::WriteLine( "\"{0}\" == \"{1}\" ?  {2}", Lower, Comparand, Lower == Comparand );
}

int main()
{
   Console::WriteLine( "This example of the String Equality operator\n"
   "generates the following output.\n" );
   CompareAndDisplay( "ijkl" );
   CompareAndDisplay( "ABCD" );
   CompareAndDisplay( "abcd" );
}

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

"abcd" == "ijkl" ?  False
"abcd" == "ABCD" ?  False
"abcd" == "abcd" ?  True
*/
// 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
*/

備註

Equality方法會定義類別的等號比較運算子運算 String 。 它會啟用如範例一節所示的程式碼。 運算子接著會呼叫靜態 Equals(String, String) 方法,此方法會執行序數 (區分大小寫和不區分文化特性的) 比較。

注意

Visual Basic 編譯器不會將相等運算子解析為方法的呼叫 Equality 。 相反地,等號比較運算子會包裝對 Operators.CompareString 方法的呼叫。

適用於