C# | Check if two StringBuilder objects are Equal

StringBuilder.Equals Method is used to check whether this instance is equal to a specified object.

Syntax: public bool Equals (System.Text.StringBuilder sb);
Here, sb is an object to compare with this instance, or null.

Return Value: It will return true if this instance and sb have an equal string, Capacity, and MaxCapacity values; otherwise, false.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to if a StringBuilder object
// is equal to another StringBuilder object
using System;
using System.Text;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st1 = new StringBuilder("GeeksforGeeks");
  
        // Checking whether st1 is
        // equal to itself or not
        Console.WriteLine(st1.Equals(st1));
    }
}

chevron_right


Output:



True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to if a StringBuilder object
// is equal to another StringBuilder object
using System;
using System.Text;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st1 = new StringBuilder("GeeksforGeeks");
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st2 = new StringBuilder("GFG");
  
        // Checking whether st1 is
        // equal to st2 or not
        Console.WriteLine(st1.Equals(st2));
  
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder st3 = new StringBuilder();
  
        // Assigning st2 to st3
        st2 = st3;
  
        // Checking whether st3 is
        // equal to st2 or not
        Console.WriteLine(st3.Equals(st2));
    }
}

chevron_right


Output:

False
True

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.