Check if two StringBuilder objects are Equal in C#


To check if two StringBuilder objects are equal, the code is as follows −

Example

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main(String[] args){
      StringBuilder strBuilder1 = new StringBuilder("Tim");
      StringBuilder strBuilder2 = new StringBuilder("Tom");
      StringBuilder strBuilder3 = new StringBuilder();
      strBuilder2 = strBuilder3;
      Console.WriteLine("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2));
   }
}

Output

This will produce the following output −

Is StringBuilder3 equal to StringBuilder2? = True

Example

Let us see another example −

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main(String[] args){
      StringBuilder strBuilder1 = new StringBuilder("Jacob");
      StringBuilder strBuilder2 = new StringBuilder("Jacob");
      Console.WriteLine("Is StringBuilder1 equal to StringBuilder2? = "+strBuilder2.Equals(strBuilder1));
   }
}

Output

This will produce the following output −

Is StringBuilder1 equal to StringBuilder2? = True

Updated on: 05-Dec-2019

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements