StringBuilder.Equals メソッド

定義

オーバーロード

Equals(ReadOnlySpan<Char>)

このインスタンスの文字が、指定された読み取り専用の文字範囲内の文字と同じであるかどうかを示す値を返します。

Equals(StringBuilder)

このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。

Equals(ReadOnlySpan<Char>)

ソース:
StringBuilder.cs
ソース:
StringBuilder.cs
ソース:
StringBuilder.cs

このインスタンスの文字が、指定された読み取り専用の文字範囲内の文字と同じであるかどうかを示す値を返します。

public:
 bool Equals(ReadOnlySpan<char> span);
public bool Equals (ReadOnlySpan<char> span);
override this.Equals : ReadOnlySpan<char> -> bool
Public Function Equals (span As ReadOnlySpan(Of Char)) As Boolean

パラメーター

span
ReadOnlySpan<Char>

現在のインスタンスと比較する文字範囲。

戻り値

このインスタンスの文字と span が同じである場合は true。それ以外の場合は false

注釈

メソッドは Equals 序数比較を実行して、現在のインスタンス内の 文字と span が等しいかどうかを判断します。

適用対象

Equals(StringBuilder)

ソース:
StringBuilder.cs
ソース:
StringBuilder.cs
ソース:
StringBuilder.cs

このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。

public:
 bool Equals(System::Text::StringBuilder ^ sb);
public bool Equals (System.Text.StringBuilder sb);
public bool Equals (System.Text.StringBuilder? sb);
override this.Equals : System.Text.StringBuilder -> bool
Public Function Equals (sb As StringBuilder) As Boolean

パラメーター

sb
StringBuilder

対象のインスタンスと比較する対象のオブジェクト、または null

戻り値

このインスタンスと sb が同じ文字列、Capacity 値、および MaxCapacity 値を保持している場合は true。それ以外の場合は false

次のコードでは、 メソッドを Equals 使用して、2 つの StringBuilder オブジェクトが等しいかどうかを確認します。 メソッドは、各オブジェクトに対して小さな変更が行われた後に繰り返し呼び出され、結果がコンソールに表示されます。

using namespace System;
using namespace System::Text;
int main()
{
   StringBuilder^ sb1 = gcnew StringBuilder( "abc" );
   StringBuilder^ sb2 = gcnew StringBuilder( "abc",16 );
   Console::WriteLine();
   Console::WriteLine( "a1) sb1->Length = {0}, sb1->Capacity = {1}", sb1->Length, sb1->Capacity );
   Console::WriteLine( "a2) sb2->Length = {0}, sb2->Capacity = {1}", sb2->Length, sb2->Capacity );
   Console::WriteLine( "a3) sb1 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
   Console::WriteLine( "a4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
   Console::WriteLine();
   Console::WriteLine( "Ensure sb1 has a capacity of at least 50 characters." );
   sb1->EnsureCapacity( 50 );
   Console::WriteLine();
   Console::WriteLine( "b1) sb1->Length = {0}, sb1->Capacity = {1}", sb1->Length, sb1->Capacity );
   Console::WriteLine( "b2) sb2->Length = {0}, sb2->Capacity = {1}", sb2->Length, sb2->Capacity );
   Console::WriteLine( "b3) sb1 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
   Console::WriteLine( "b4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
   Console::WriteLine();
   Console::WriteLine( "Set the length of sb1 to zero." );
   Console::WriteLine( "Set the capacity of sb2 to 51 characters." );
   sb1->Length = 0;
   sb2->Capacity = 51;
   Console::WriteLine();
   Console::WriteLine( "c1) sb1->Length = {0}, sb1->Capacity = {1}", sb1->Length, sb1->Capacity );
   Console::WriteLine( "c2) sb2->Length = {0}, sb2->Capacity = {1}", sb2->Length, sb2->Capacity );
   Console::WriteLine( "c3) sb1 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
   Console::WriteLine( "c4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
}

/*
The example displays the following output:

a1) sb1->Length = 3, sb1->Capacity = 16
a2) sb2->Length = 3, sb2->Capacity = 16
a3) sb1 = "abc", sb2 = "abc"
a4) sb1 equals sb2: True

Ensure sb1 has a capacity of at least 50 characters.

b1) sb1->Length = 3, sb1->Capacity = 50
b2) sb2->Length = 3, sb2->Capacity = 16
b3) sb1 = "abc", sb2 = "abc"
b4) sb1 equals sb2: False

Set the length of sb1 to zero.
Set the capacity of sb2 to 51 characters.

c1) sb1->Length = 0, sb1->Capacity = 50
c2) sb2->Length = 3, sb2->Capacity = 51
c3) sb1 = "", sb2 = "abc"
c4) sb1 equals sb2: False
*/
using System;
using System.Text;

class Sample
{
    public static void Main()
    {
    StringBuilder sb1 = new StringBuilder("abc");
    StringBuilder sb2 = new StringBuilder("abc", 16);

    Console.WriteLine();
    Console.WriteLine("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
    Console.WriteLine("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
    Console.WriteLine("a3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
                           sb1.ToString(),       sb2.ToString());
    Console.WriteLine("a4) sb1 equals sb2: {0}", sb1.Equals(sb2));

    Console.WriteLine();
    Console.WriteLine("Ensure sb1 has a capacity of at least 50 characters.");
    sb1.EnsureCapacity(50);

    Console.WriteLine();
    Console.WriteLine("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
    Console.WriteLine("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
    Console.WriteLine("b3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
                           sb1.ToString(),       sb2.ToString());
    Console.WriteLine("b4) sb1 equals sb2: {0}", sb1.Equals(sb2));

    Console.WriteLine();
    Console.WriteLine("Set the length of sb1 to zero.");
    Console.WriteLine("Set the capacity of sb2 to 51 characters.");
    sb1.Length = 0;
    sb2.Capacity = 51;

    Console.WriteLine();
    Console.WriteLine("c1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
    Console.WriteLine("c2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
    Console.WriteLine("c3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
                           sb1.ToString(),       sb2.ToString());
    Console.WriteLine("c4) sb1 equals sb2: {0}", sb1.Equals(sb2));
    }
}
/*
The example displays the following output:

a1) sb1.Length = 3, sb1.Capacity = 16
a2) sb2.Length = 3, sb2.Capacity = 16
a3) sb1.ToString() = "abc", sb2.ToString() = "abc"
a4) sb1 equals sb2: True

Ensure sb1 has a capacity of at least 50 characters.

b1) sb1.Length = 3, sb1.Capacity = 50
b2) sb2.Length = 3, sb2.Capacity = 16
b3) sb1.ToString() = "abc", sb2.ToString() = "abc"
b4) sb1 equals sb2: False

Set the length of sb1 to zero.
Set the capacity of sb2 to 51 characters.

c1) sb1.Length = 0, sb1.Capacity = 50
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/
open System.Text

let sb1 = StringBuilder "abc"
let sb2 = StringBuilder("abc", 16)

printfn $"a1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}"
printfn $"a2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}"
printfn $"a3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\""
printfn $"a4) sb1 equals sb2: {sb1.Equals sb2}"

printfn "\nEnsure sb1 has a capacity of at least 50 characters."
sb1.EnsureCapacity 50 |> ignore

printfn $"\nb1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}"
printfn $"b2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}"
printfn $"b3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\""
printfn $"b4) sb1 equals sb2: {sb1.Equals sb2}"

printfn "\nSet the length of sb1 to zero."
printfn "Set the capacity of sb2 to 51 characters."
sb1.Length <- 0
sb2.Capacity <- 51

printfn $"\nc1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}"
printfn $"c2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}"
printfn $"c3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\""
printfn $"c4) sb1 equals sb2: {sb1.Equals sb2}"

// The example displays the following output:
//       a1) sb1.Length = 3, sb1.Capacity = 16
//       a2) sb2.Length = 3, sb2.Capacity = 16
//       a3) sb1.ToString() = "abc", sb2.ToString() = "abc"
//       a4) sb1 equals sb2: True
//
//       Ensure sb1 has a capacity of at least 50 characters.
//       
//       b1) sb1.Length = 3, sb1.Capacity = 50
//       b2) sb2.Length = 3, sb2.Capacity = 16
//       b3) sb1.ToString() = "abc", sb2.ToString() = "abc"
//       b4) sb1 equals sb2: False
//       
//       Set the length of sb1 to zero.
//       Set the capacity of sb2 to 51 characters.
//       
//       c1) sb1.Length = 0, sb1.Capacity = 50
//       c2) sb2.Length = 3, sb2.Capacity = 51
//       c3) sb1.ToString() = "", sb2.ToString() = "abc"
//       c4) sb1 equals sb2: False
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb1 As New StringBuilder("abc")
      Dim sb2 As New StringBuilder("abc", 16)
      
      Console.WriteLine()
      Console.WriteLine("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
      Console.WriteLine("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
      Console.WriteLine("a3) sb1.ToString() = ""{0}"", sb2.ToString() = ""{1}""", _
                             sb1.ToString(),           sb2.ToString())
      Console.WriteLine("a4) sb1 equals sb2: {0}", sb1.Equals(sb2))
      
      Console.WriteLine()
      Console.WriteLine("Ensure sb1 has a capacity of at least 50 characters.")
      sb1.EnsureCapacity(50)
      
      Console.WriteLine()
      Console.WriteLine("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
      Console.WriteLine("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
      Console.WriteLine("b3) sb1.ToString() = ""{0}"", sb2.ToString() = ""{1}""", _
                             sb1.ToString(),           sb2.ToString())
      Console.WriteLine("b4) sb1 equals sb2: {0}", sb1.Equals(sb2))
      
      Console.WriteLine()
      Console.WriteLine("Set the length of sb1 to zero.")
      Console.WriteLine("Set the capacity of sb2 to 51 characters.")
      sb1.Length = 0
      sb2.Capacity = 51
      
      Console.WriteLine()
      Console.WriteLine("c1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
      Console.WriteLine("c2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
      Console.WriteLine("c3) sb1.ToString() = ""{0}"", sb2.ToString() = ""{1}""", _
                             sb1.ToString(),           sb2.ToString())
      Console.WriteLine("c4) sb1 equals sb2: {0}", sb1.Equals(sb2))
   End Sub 
End Class
'The example displays the following output:
'       a1) sb1.Length = 3, sb1.Capacity = 16
'       a2) sb2.Length = 3, sb2.Capacity = 16
'       a3) sb1.ToString() = "abc", sb2.ToString() = "abc"
'       a4) sb1 equals sb2: True
'       
'       Ensure sb1 has a capacity of at least 50 characters.
'       
'       b1) sb1.Length = 3, sb1.Capacity = 50
'       b2) sb2.Length = 3, sb2.Capacity = 16
'       b3) sb1.ToString() = "abc", sb2.ToString() = "abc"
'       b4) sb1 equals sb2: False
'       
'       Set the length of sb1 to zero.
'       Set the capacity of sb2 to 51 characters.
'       
'       c1) sb1.Length = 0, sb1.Capacity = 50
'       c2) sb2.Length = 3, sb2.Capacity = 51
'       c3) sb1.ToString() = "", sb2.ToString() = "abc"
'       c4) sb1 equals sb2: False

注釈

.NET Frameworkおよび .NET Core 2.2 以前のバージョン: 現在のインスタンスと sb が等しい場合、文字列、、Capacityおよび MaxCapacity の値が等しくなります。 メソッドは Equals 序数比較を使用して、文字列が等しいかどうかを判断します。

.NET Core 3.0 以降のバージョン: 現在の インスタンス と sb は、両方 StringBuilder のオブジェクトに割り当てられた文字列が同じ場合は等しくなります。 等しいかどうかを判断するために、 Equals メソッドは序数比較を使用します。 プロパティと MaxCapacity プロパティのCapacity値は、比較では使用されません。

適用対象