다음을 통해 공유


StringBuilder.Equals 메서드

정의

오버로드

Name Description
Equals(ReadOnlySpan<Char>)

이 인스턴스의 문자가 지정된 읽기 전용 문자 범위의 문자와 같은지 여부를 나타내는 값을 반환합니다.

Equals(StringBuilder)

이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

Equals(ReadOnlySpan<Char>)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
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>

현재 인스턴스와 비교할 문자 범위입니다.

반환

이 인스턴스의 문자가 같으면 , 그렇지 않으면 .

설명

메서드는 Equals 서수 비교를 수행하여 현재 인스턴스의 문자가 span 같은지 여부를 확인합니다.

적용 대상

Equals(StringBuilder)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
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.

반환

.NET(Core) true 에서 이 인스턴스의 문자가 같으면 이고 sb , false그렇지 않으면 .

.NET Framework에서 true 이 인스턴스에 문자열 및 sb 값이 같으면 이고MaxCapacity, Capacityfalse그렇지 않으면 .

예제

다음 코드는 메서드를 Equals 사용하여 두 StringBuilder 개체가 같은지 여부를 확인합니다. 메서드는 각 개체를 약간 변경한 후 반복적으로 호출되고 결과가 콘솔에 표시됩니다.

using System;
using System.Text;

StringBuilder sb1 = new("abc");
StringBuilder sb2 = new("abc", 16);

Console.WriteLine();
Console.WriteLine($"a1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}");
Console.WriteLine($"a2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}");
Console.WriteLine($"a3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\"");
Console.WriteLine($"a4) sb1 equals sb2: {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 = {sb1.Length}, sb1.Capacity = {sb1.Capacity}");
Console.WriteLine($"b2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}");
Console.WriteLine($"b3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\"");
Console.WriteLine($"b4) sb1 equals sb2: {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 = {sb1.Length}, sb1.Capacity = {sb1.Capacity}");
Console.WriteLine($"c2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}");
Console.WriteLine($"c3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\"");
Console.WriteLine($"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: True (False on .NET Framework)

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: True (False on .NET Framework)
//       
//       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: True (False on .NET Framework)
'       
'       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 및 이전 버전: 현재 인스턴스이며 문자열 CapacitysbMaxCapacity 값이 같으면 같습니다. 메서드는 Equals 서수 비교를 사용하여 문자열이 같은지 여부를 확인합니다.

.NET Core 3.0 이상 버전: 현재 인스턴스이며 sbStringBuilder 개체에 할당된 문자열이 같으면 동일합니다. 같음을 확인하기 위해 메서드는 Equals 서수 비교를 사용합니다. 및 MaxCapacity 속성 값은 Capacity 비교에 사용되지 않습니다.

적용 대상