Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

StringBuilder in C#

In C#, the string type is immutable. It means a string cannot be changed once created. For example, a new string, “Hello World!” will occupy a memory space on the heap. Now, by changing the initial string “Hello World!” to “Hello World! from InterviewBit” will create a new string object on the memory heap instead of modifying an original string at the same memory address. This behavior would hinder the performance if the original string changed multiple times by replacing, appending, removing, or inserting new strings in the original string.

To solve this problem, C# introduced the StringBuilder in the System.Text namespace. The StringBuilder doesn’t create a new object in the memory but dynamically expands memory to accommodate the modified string.


Creating a new StringBuilder

You can create an object of the StringBuilder class using the new keyword and passing an initial string. The following example demonstrates creating StringBuilder objects.

using System.Text; // include at the top
            
StringBuilder sb = new StringBuilder(); //string will be appended later
//or
StringBuilder sb = new StringBuilder("Hello World!");

Optionally, you can also specify the maximum capacity of the StringBuilder object using overloaded constructors.


Retrieve String from StringBuilder

The StringBuilder is not the String. Use the ToString() method to retrieve a string from the StringBuilder object.

StringBuilder sb = new StringBuilder("Hello World!");

String greet = sb.ToString(); //returns "Hello World!"

Add/Append String to StringBuilder

Use the Append() method to append a string at the end of the current StringBuilder object. If a StringBuilder does not contain any string yet, it will add it. The AppendLine() method append a string with the newline character at the end.

StringBuilder sb = new StringBuilder();
sb.Append("Hello ");
sb.AppendLine("World!");
sb.AppendLine("Hello C#");
Console.WriteLine(sb);

Output:

Hello World!

Hello C#.


Some other commonly used StringBuilder functions

  • StringBuilder.Insert: Inserts a string or object into the specified index of the current StringBuilder.
  • StringBuilder.Remove: Removes a specified number of characters from the current StringBuilder.
  • StringBuilder.Replace: Replaces a specified character at a specified index.

Task

In the editor below, perform the different tasks as directed.

Start solving StringBuilder in C# on Interview Code Editor
Hints
  • Solution Approach
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test