次の方法で共有


String.Equals メソッド

2 つの String オブジェクトの値が同一かどうかを判断します。

オーバーロードの一覧

String のこのインスタンスと、指定したオブジェクトの値が同一かどうかを判断します。指定したオブジェクトは String である必要があります。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Overrides Public Function Equals(Object) As Boolean

[C#] public override bool Equals(object);

[C++] public: bool Equals(Object*);

[JScript] public override function Equals(Object) : Boolean;

このインスタンスと、指定した String の値が同一かどうかを判断します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function Equals(String) As Boolean

[C#] public bool Equals(string);

[C++] public: bool Equals(String*);

[JScript] public function Equals(String) : Boolean;

指定した 2 つの String オブジェクトの値が同一かどうかを判断します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function Equals(String, String) As Boolean

[C#] public static bool Equals(string, string);

[C++] public: static bool Equals(String*, String*);

[JScript] public static function Equals(String, String) : Boolean;

使用例

[Visual Basic, C#, C++] メモ   ここでは、Equals のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
' Sample for String.Equals(Object)
'            String.Equals(String)
'            String.Equals(String, String)
Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb As New StringBuilder("abcd")
      Dim str1 As [String] = "abcd"
      Dim str2 As [String] = Nothing
      Dim o2 As [Object] = Nothing
      
      Console.WriteLine()
      Console.WriteLine(" *  The value of String str1 is '{0}'.", str1)
      Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString())
      
      Console.WriteLine()
      Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.")
      Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb))
      
      Console.WriteLine()
      Console.WriteLine("1b) String.Equals(Object). Object is a String.")
      str2 = sb.ToString()
      o2 = str2
      Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2)
      Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2))
      
      Console.WriteLine()
      Console.WriteLine(" 2) String.Equals(String)")
      Console.WriteLine(" *  The value of String str2 is '{0}'.", str2)
      Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2))
      
      Console.WriteLine()
      Console.WriteLine(" 3) String.Equals(String, String)")
      Console.WriteLine("    Is str1 equal to str2?: {0}", [String].Equals(str1, str2))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
' *  The value of String str1 is 'abcd'.
' *  The value of StringBuilder sb is 'abcd'.
'
'1a) String.Equals(Object). Object is a StringBuilder, not a String.
'    Is str1 equal to sb?: False
'
'1b) String.Equals(Object). Object is a String.
' *  The value of Object o2 is 'abcd'.
'    Is str1 equal to o2?: True
'
' 2) String.Equals(String)
' *  The value of String str2 is 'abcd'.
'    Is str1 equal to str2?: True
'
' 3) String.Equals(String, String)
'    Is str1 equal to str2?: True
'

[C#] 
// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
using System;
using System.Text;

class Sample {
    public static void Main() {
    StringBuilder sb = new StringBuilder("abcd");
    String      str1 = "abcd";
    String      str2 = null;
    Object    o2   = null;

    Console.WriteLine();
    Console.WriteLine(" *  The value of String str1 is '{0}'.", str1);
    Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString());

    Console.WriteLine();
    Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.");
    Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb));

    Console.WriteLine();
    Console.WriteLine("1b) String.Equals(Object). Object is a String.");
    str2 = sb.ToString();
    o2   = str2;
    Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2);
    Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2));

    Console.WriteLine();
    Console.WriteLine(" 2) String.Equals(String)");
    Console.WriteLine(" *  The value of String str2 is '{0}'.", str2);
    Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2));

    Console.WriteLine();
    Console.WriteLine(" 3) String.Equals(String, String)");
    Console.WriteLine("    Is str1 equal to str2?: {0}", String.Equals(str1, str2));
    }
}
/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*/

[C++] 
// Sample for String::Equals(Object)
//            String::Equals(String)
//            String::Equals(String, String)
#using <mscorlib.dll>

using namespace System;
using namespace System::Text;

int main() {
    StringBuilder* sb = new StringBuilder(S"abcd");
    String* str1 = S"abcd";
    String* str2 = 0;
    Object* o2 = 0;

    Console::WriteLine();
    Console::WriteLine(S" *  The value of String str1 is '{0}'.", str1);
    Console::WriteLine(S" *  The value of StringBuilder sb is '{0}'.", sb);

    Console::WriteLine();
    Console::WriteLine(S"1a) String::Equals(Object). Object is a StringBuilder, not a String.");
    Console::WriteLine(S"    Is str1 equal to sb?: {0}", __box(str1->Equals(sb)));

    Console::WriteLine();
    Console::WriteLine(S"1b) String::Equals(Object). Object is a String.");
    str2 = sb->ToString();
    o2   = str2;
    Console::WriteLine(S" *  The value of Object o2 is '{0}'.", o2);
    Console::WriteLine(S"    Is str1 equal to o2?: {0}", __box(str1->Equals(o2)));

    Console::WriteLine();
    Console::WriteLine(S" 2) String::Equals(String)");
    Console::WriteLine(S" *  The value of String str2 is '{0}'.", str2);
    Console::WriteLine(S"    Is str1 equal to str2?: {0}", __box(str1->Equals(str2)));

    Console::WriteLine();
    Console::WriteLine(S" 3) String::Equals(String, String)");
    Console::WriteLine(S"    Is str1 equal to str2?: {0}", __box(String::Equals(str1, str2)));
}
/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String::Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String::Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String::Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String::Equals(String, String)
    Is str1 equal to str2?: True
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

String クラス | String メンバ | System 名前空間