Compare the content of two StringBuilders

The Equals method in C# is used to compare the content of two StringBuilder objects. This method performs a character-by-character comparison of the text content stored in both StringBuilder instances.

Syntax

Following is the syntax for comparing two StringBuilder objects −

bool result = stringBuilder1.Equals(stringBuilder2);

Parameters

The Equals method takes one parameter −

  • sb − The StringBuilder object to compare with the current instance.

Return Value

The method returns a bool value −

  • true if both StringBuilder objects have the same content.

  • false if the contents are different.

Using StringBuilder.Equals() for Content Comparison

The following example demonstrates comparing two StringBuilder objects with different content −

using System;
using System.Text;

class Demo {
   static void Main() {
      // first
      StringBuilder str1 = new StringBuilder();
      str1.Append("Tim");
      str1.Append("Tom");
      str1.Append("Henry");

      // second
      StringBuilder str2 = new StringBuilder();
      str2.Append("John");
      str2.Append("David");
      str2.Append("Beth");

      // check for equality
      if (str1.Equals(str2)) {
         Console.WriteLine("Contents are equal!");
      } else {
         Console.WriteLine("Contents are unequal!");
      }
   }
}

The output of the above code is −

Contents are unequal!

Comparing StringBuilder Objects with Same Content

Here's an example where both StringBuilder objects contain the same text −

using System;
using System.Text;

class Demo {
   static void Main() {
      StringBuilder str1 = new StringBuilder();
      str1.Append("Hello");
      str1.Append("World");

      StringBuilder str2 = new StringBuilder();
      str2.Append("HelloWorld");

      Console.WriteLine("str1 content: " + str1.ToString());
      Console.WriteLine("str2 content: " + str2.ToString());

      if (str1.Equals(str2)) {
         Console.WriteLine("Contents are equal!");
      } else {
         Console.WriteLine("Contents are unequal!");
      }
   }
}

The output of the above code is −

str1 content: HelloWorld
str2 content: HelloWorld
Contents are equal!

Alternative Methods for StringBuilder Comparison

Besides the Equals method, you can also compare StringBuilder objects by converting them to strings −

using System;
using System.Text;

class Demo {
   static void Main() {
      StringBuilder str1 = new StringBuilder("Programming");
      StringBuilder str2 = new StringBuilder("Programming");

      // Method 1: Using Equals()
      bool result1 = str1.Equals(str2);
      Console.WriteLine("Using Equals(): " + result1);

      // Method 2: Converting to string and comparing
      bool result2 = str1.ToString() == str2.ToString();
      Console.WriteLine("Using ToString(): " + result2);

      // Method 3: Using string.Equals()
      bool result3 = string.Equals(str1.ToString(), str2.ToString());
      Console.WriteLine("Using string.Equals(): " + result3);
   }
}

The output of the above code is −

Using Equals(): True
Using ToString(): True
Using string.Equals(): True

Conclusion

The StringBuilder.Equals() method provides an efficient way to compare the content of two StringBuilder objects. It performs a character-by-character comparison and returns true only when both objects contain identical text content, regardless of how the content was built.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements