TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# StringBuilder Equals Method

This C# article demonstrates the Equals method on StringBuilder.

StringBuilder equals. Two StringBuilder instances may have the same string contents.

We test them for equality. The Equals instance method is ideal for this. It doesn't copy any characters or cause excessive method calls.

Example. First, we use Equals on StringBuilder. The below example shows an equality test that succeeds. The two StringBuilder objects in the example both contain the same characters "One two" and have the same capacity.

C# 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

Near the end, we see four lines that are commented out. You can test StringBuilder references for equality using "==", but this just uses the Object.Equals method, which is defined on the object type for all instances.

And: The test would not succeed because the two references are different. It does not test the character contents.

object.Equals Method

Implementation. The StringBuilder instance Equals method returns true when three conditions are true. Two objects must have the same capacity, the same MaxCapacity, and the same characters in their buffers.

True and False

Tip: If you create two StringBuilders with the same contents but with different capacities, they will not evaluate true.

Contents. If you need to test two StringBuilders that may have different capacities but the same contents, you can iterate through their characters with the indexer. You can loop through the entire Length.

Note: This requirement is unlikely, but you may need to develop a custom Equals method.

Summary. We used the Equals instance method on StringBuilder. This method is overridden and provides a powerful and efficient way to test two StringBuilders for equality. There are other ways of testing them, but they are more cumbersome.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf