C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Important: We can test StringBuilder references for equality using "==", but this uses the Object.Equals method, which is defined on the object type.
And: The test would not succeed because the two references are different. It does not test the character contents.
ObjectC# program that uses StringBuilder Equals method
using System;
using System.Text;
class Program
{
static void Main()
{
//
// Create two StringBuilders with the same contents.
//
StringBuilder builder1 = new StringBuilder();
builder1.Append("One ");
builder1.Append("two");
StringBuilder builder2 = new StringBuilder();
builder2.Append("One ");
builder2.Append("two");
//
// See if the StringBuilder contents are equal.
//
if (builder1.Equals(builder2))
{
Console.WriteLine("Equal");
}
// if (builder1 == builder2)
// {
// Console.WriteLine("Equal");
// }
}
}
Output
Equal
Tip: If you create two StringBuilders with the same contents but with different capacities, they will not evaluate true.
Note: This requirement is unlikely, but you may need to develop a custom Equals method.