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

傳回

true 若 的 a 值與 的 b值相同;否則, false

範例

以下範例示範了等號運算子。

// 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
*/
// Example for the String Equality operator.
printfn "This example of the String Equality operator\ngenerates the following output.\n"

let compareAndDisplay comparand =
    let lower = "abcd"
    printfn $"\"%s{lower}\" == \"%s{comparand}\" ?  {lower = comparand}"

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
*)

備註

Equality 方法定義了該類別等號運算 String 子的運算。 它能啟用範例章節所示的程式碼。 運算子則呼叫靜態 Equals(String, String) 方法,進行序數比較(大小寫區分且不影響培養)。

Note

Visual Basic編譯器不會將等號運算子解析為呼叫 Equality 方法。 取而代之的是,等號運算子會將呼叫包裹到該 Operators.CompareString 方法。

適用於