A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/stringbuilder-in-c-sharp/ below:

C# StringBuilder - GeeksforGeeks

C# StringBuilder

Last Updated : 11 Jan, 2025

StringBuilder is a Dynamic Object. It doesn’t create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.A String object is immutable, i.e. a String cannot be changed once created. To avoid string replacing, appending, removing or inserting new strings in the initial string C# introduce StringBuilder concept.

Declaration and Initialization of StringBuilder

StringBuilder can be declared and initialized the same way as class,

StringBuilder s = new StringBuilder();

or

StringBuilder s = new StringBuilder("GeeksforGeeks");

“s” is the object of StringBuilder class. Also, we can pass a string value(here “GeeksforGeeks”) as an argument to the constructor of StringBuilder.

Defining the capacity of StringBuilder

Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the StringBuilder object.

StringBuilder s = new StringBuilder(20);

or

StringBuilder s = new StringBuilder("GeeksForGeeks", 20);


Here,

Important Methods of StringBuilder Class

Method with Syntax

Description

StringBuilder.Append(string value)

The Append method can be used to add or append a string value of an object to the end of a string represented by the current StringBuilder object.

StringBuilder.AppendFormat()

This method uses to format the input string into the specified format and then append it.

StringBuilder.Insert(int index, string value)

This method inserts the string at specified index in StringBuilder object.

StringBuilder.Remove(int start, int length)

This method removes the specified number of characters from the current StringBuilder object.

StringBuilder.Replace(old_val, new_val)

This method is used to replace characters within the StringBuilder object with another specified character.

Example 1: For appending string value in StringBuilder

C#
// Adding element in StringBuilder Object
using System; 
using System.Text; 

class Geeks 
{ 
	// Main Method
	public static void Main() 
	{ 
		// "20" is capacity 
		StringBuilder s = new StringBuilder("HELLO ", 20); 
		
		s.Append("GFG"); 

		// after printing "GEEKS" 
		// a new line append 
		s.AppendLine("GEEKS"); 
		
		s.Append("GeeksForGeeks"); 
		Console.WriteLine(s); 
	} 
} 

Output
HELLO GFGGEEKS
GeeksForGeeks

Example 2: Format the input string into the specified format and then append it.

C#
// Adding the Formatted String in
// StringBuilder Object
using System; 
using System.Text; 

class Geeks 
{ 
	// Main Method 
	public static void Main() 
	{ 
		StringBuilder s = new StringBuilder("Your total amount is "); 

		// using the method 
		s.AppendFormat("{0:C} ", 50); 

		Console.WriteLine(s); 
	} 
} 

Output
Your total amount is ?50.00 

Example 3: Insert string at specified value in StringBuilder object

C#
// Inserting String at specified index
// position/index in StringBuilder 
using System; 
using System.Text; 

class Geeks 
{ 
	// Main Method 
	public static void Main() 
	{ 
		// "20" is capacity 
		StringBuilder s = new StringBuilder("HELLO ", 20); 
		
		// "GEEKS" insert after 6th index 
		s.Insert(6, "GEEKS"); 
		
		Console.WriteLine(s); 
	} 
} 

Example 4: Removes the specified number of characters from the StringBuilder Object

C#
// Removing elements from particular
// indexes in StringBuilder Object
using System; 
using System.Text; 

class Geeks
{ 
	// Main Method 
	public static void Main() 
	{ 
		// "20" is capacity 
		StringBuilder s = new StringBuilder("GeeksForGeeks", 20); 

		// remove starts from index 5 
		// and remove happes 3 index 
		// after index 5 
		s.Remove(5, 3); 
		
		Console.WriteLine(s); 
	} 
} 

Example 5: Replace characters within the StringBuilder Object

C#
// Replace Elements in StringBuilder Object
using System; 
using System.Text; 

class Geeks
{ 
	// Main Method 
	public static void Main() 
	{ 

		// "20" is capacity 
		StringBuilder s = new StringBuilder("GFG Geeks ", 20); 
		
		// Replace "GFG" with "Geeks For" 
		s.Replace("GFG", "Geeks For"); 

		Console.WriteLine(s); 
	} 
} 


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4