Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# StringBuilder


Share

StringBuilder

In the String Methods chapter, you learned that methods like Replace() do not change the original string. They return a new string. The + operator also creates a new string every time you use it. In C#, a string cannot be changed after it is created.

A StringBuilder is text that can be changed. You can add, insert, replace and remove text, and C# keeps changing the same text. Think of it like a document that you keep editing, instead of writing a new copy for every change.

To use StringBuilder, add this line at the top of your program, below using System;:

Example

// Include the namespace that holds StringBuilder
using System.Text;

Now you can create a StringBuilder and add text to it:

Example

StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World");
Console.WriteLine(sb);

Try it Yourself »

Example explained

  • new StringBuilder("Hello") creates a StringBuilder with the text "Hello".
  • sb.Append(" World") adds " World" to the end of the text.
  • Console.WriteLine(sb) prints the text Hello World.

Add Text with Append()

The Append() method adds text to the end. You can call it as many times as you like, and it also works with numbers.

Empty parentheses, new StringBuilder(), create an empty StringBuilder:

Example

StringBuilder sb = new StringBuilder();
sb.Append("Liam");
sb.Append(" has ");
sb.Append(3);
sb.Append(" apples");
Console.WriteLine(sb);

Try it Yourself »

Just like a string, a StringBuilder has a Length property, which returns the number of characters:

Example

StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World");
Console.WriteLine(sb.Length);  // 11

Try it Yourself »



Insert, Replace and Remove

A StringBuilder can also change text in the middle. Remember that positions start at 0, just like in Access Strings:

  • Insert(position, text) inserts text at a position
  • Replace(oldText, newText) replaces every oldText with newText
  • Remove(position, count) removes a number of characters (count), starting at a position

Example

StringBuilder sb = new StringBuilder("I like cats");
sb.Insert(7, "black ");      // I like black cats
sb.Replace("cats", "dogs");  // I like black dogs
sb.Remove(7, 6);             // I like dogs
Console.WriteLine(sb);

Try it Yourself »

Remove(7, 6) removes 6 characters, starting at position 7. That is the text "black " (5 letters and a space).


Convert to a String

Use ToString() to turn a StringBuilder into a normal string. You need this when you want to store the text in a string variable, or use string methods like ToUpper():

Example

StringBuilder sb = new StringBuilder("hello");
sb.Append(" world");
string text = sb.ToString();
Console.WriteLine(text.ToUpper());  // HELLO WORLD

Try it Yourself »


String vs. StringBuilder

Use a string for text that does not change, like a name or a message.

Use a StringBuilder when you change the text many times. It is faster, because C# does not have to create a new string for every change.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.