StringBuilder in C#

A String is immutable, meaning String cannot be changed once created. For example, new string "Hello World!!" will occupy a memory space on the heap. Now, by changing the initial string "Hello World!!" to "Hello World!! from Tutorials Teacher" will create a new string object on the memory heap instead of modifying the initial string at the same memory address. This behaviour will hinder the performance if the same string changes multiple times by replacing, appending, removing or inserting new strings in the initial string.
To solve this problem, C# introduced StringBuilder. StringBuilder is a dynamic object that allows you to expand the number of characters in the string. It doesn t create a new object in the memory but dynamically expands memory to accommodate the modified string.
A StringBuilder object is not a string but rather an auxiliary object used for manipulating characters. It contains a buffer, typically initialized with a string but usually larger than that string. This buffer can be manipulated in place without creating a new string: You can insert, append, remove, and replace characters. When you re done manipulating the characters, use StringBuilder s ToString method to extract the finished string from it.
Both String and StringBuilder contain Unicode characters of type Char and support an indexer that returns Char. Because the String class is immutable, its indexer is read-only, but the StringBuilder indexer is readable/writeable.

Example of StringBuilder is
StringBuilder sb = new StringBuilder();
Or
StringBuilder sb = new StringBuilder("Hello World");
You can give an initial capacity of characters by passing an int value in the constructor. For example, the following will allocate memory of 50 characters sequentially on the memory heap. The memory allocation automatically expands once it reaches the capacity.
StringBuilder sb = new StringBuilder(50);

Usage of StringBuilder:
You can use StringBuilder for string operations depends on the frequency of the string modification. If you modify the string frequently-in operations like reassigning, appending, removing, and replacing some characters-you should choose the StringBuilder class.

Methods of StringBuilder:

Method Name Description
StringBuilder.Append(valueToAppend) Appends the passed values to the end of the current StringBuilder object.

StringBuilder.Insert(index, valueToAppend) Inserts a string at the specified index of the current StringBuilder object.

StringBuilder.Remove(int startIndex, int length) Removes the specified number of characters from the given starting position of the current StringBuilder object.

StringBuilder.Replace(oldValue, newValue) Replaces characters with new characters.



Example of Insert() Method:
StringBuilder sb = new StringBuilder("Hello ",50);
sb.Append("World!");

Example of Append() Method:
StringBuilder sb = new StringBuilder("Hello ",50);
sb.Insert("World!");

Example of Remove() Method:
StringBuilder sb = new StringBuilder("Hello ",50);
sb.Remove(6, 7); Console.WriteLine(sb);

Example of Replace() Method:
StringBuilder sb = new StringBuilder("Hello World",50);
sb.Replace("World", "C#"); Console.WriteLine(sb);

Example of ToString() Method:
StringBuilder sb = new StringBuilder("Hello ",50);
string stvar = sb.ToString();

Read More

आइये जानते हैं Android 11 के बेस्ट 11 फीचर्स

आपके स्मार्टफोंस को और भी स्मार्ट बनाते हुए तथा मोबाइल तकनीक को और भी एडवांस करते हुए टेक दिग्गज़ Google ने अपना नया ऑपरेटिंग सिस्टम Android 11 दुनिया ...

Properties in C#

In C#, properties are nothing but natural extension of data fields. They are usually known as smart fields in C# community. We know that data encaps ...

C# Hashtable

The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the ...

C# Queue

C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO style (First In First Out), exactly opposit ...

C# Stack

C# includes a special type of collection which stores elements in LIFO style(Last In First Out). C# includes a generic and non-generic Stack. Here, y ...

C# SortedList

The SortedList collection stores key-value pairs in the ascending order of key by default. SortedList class implements IDictionary & ICollection inter ...

C# ArrayList

ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automa ...

Collection in C#

C# includes specialized classes that hold many values or objects in a specific series, that are called collection. Collection classes are specialized ...

C# File Stream I/O

A file is a collection of data stored in a disk with a specific name and a directory path. C# includes static File class to perform I/ ...

Exception Handling in C#

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while ...

Recent Posts






















Like us on Facebook