---
title: "String Builder"  
description: "We know that string objects are immutable. The term Immutable means once the object string is created then it can’t be modified. Now take an example t"  
author: "Manish Kumar"  
published: 2017-02-18  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11295/string-builder  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# String Builder

We know that [string objects are immutable](https://www.mindstick.com/interview/2435/why-string-objects-are-immutable-in-java). The term Immutable means once the object string is created then it can’t be modified. Now take an example to understand string builder suppose a string “Hello” is created so it will occupy its space on the [memory heap](https://www.mindstick.com/forum/158218/what-is-a-memory-heap-and-how-is-it-used-in-memory-management). Now we want to change or modify it for some reason to “**Hello Mind stick**” then it will not modify the initial one it will create another object of “Hello Mind Stick” it will create new [string object](https://www.mindstick.com/blog/383/javascript-string-object) on the memory heap instead of modifying.\

This is a drawback of string. String Builder solves all this type of issue. String Builder is mute. String Builder is a string that can be modified. It is a dynamic property it expand the number of [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) in string.

**Now we will take two diagram to understand [string and string Builder](https://www.mindstick.com/forum/85/difference-between-string-and-string-builder).**

![String Builder](https://www.mindstick.com/blogs/3491dbbe-a1bf-496c-82f2-8592ca8ca3f4/images/ac8354b5-a266-4d99-81d9-b6e36f3d5fbf.png)\

**In the above diagram it is clear that string can’t change it make another heap.**

![String Builder](https://www.mindstick.com/blogs/3491dbbe-a1bf-496c-82f2-8592ca8ca3f4/images/990e562d-631f-4304-a1d0-fa536f3a1832.png)\

It is faster than string we need to [concatenate](https://www.mindstick.com/interview/1431/how-do-you-concatenate-strings-in-mysql).

**Method of string builder.**

**1-**Append()

**2-**AppendFormat()

**3-**Insert()

**4-**Remove()

**5-**Replace()

**[Initialization](https://www.mindstick.com/interview/160/what-is-the-difference-between-assignment-and-initialization) of [stringBuilder](https://www.mindstick.com/blog/99/stringbuilder-class-in-c-sharp). It is same as the class.**

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using
System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Hello Mindstick!!",50);            Console.WriteLine(sb);        }    }} 
```

**Append method is use to concatenate string.**

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using
System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Hello Mindstick!!",50);            sb.Append("Civil Lines");                       Console.WriteLine(sb);        }    }} 
```

**AppendLine method is use to append string in the next line.**

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Hello Mindstick!!",50);            sb.Append("Civil Lines");            sb.AppendLine("Software Development");            Console.WriteLine(sb);        }    }} 
```

**Appendformat()**

It is use to format in specific format.

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Salary above!!");            sb.AppendFormat("{0:C}",500);            sb.AppendLine("Software Development");            Console.WriteLine(sb);        }    }} 
```

**Insert()**

Insert method is use to insert string at specific position.

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Civil line Allahabad!!");            sb.Insert(11, "Takshandmarg ");                        Console.WriteLine(sb);        }    }} 
```

**Remove()**

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using
System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Civil line
Allahabad!!");            sb.Remove(6, 4);                        Console.WriteLine(sb);        }    }} 
```

**\**

**Replace()**

Replaces all string with new string.

```
using System;using
System.Collections.Generic;using System.Linq;using System.Text;using
System.Threading.Tasks; namespace stringbuffer{    class Program    {        static void Main(string[] args)        {            StringBuilder sb = new StringBuilder("Civilline Allahabad!!");            sb.Replace("Allahabad", "Mindstick");                        Console.WriteLine(sb);        }    }}
```

---

Original Source: https://www.mindstick.com/blog/11295/string-builder

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
