C# StringBuilder Insert() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
StringBuilder

Definition

In C#, StringBuilder.Insert(index, value) puts a value into the middle of the text, starting at the given position. Everything that was at that position or later moves to the right to make room. Positions count from 0, like string indexes.

Insert(0, ...) adds at the very start, and Insert(sb.Length, ...) adds at the end, which is the same as Append(). Unlike string.Insert, which returns a new string, this changes the builder itself and returns it for chaining.

An instance method of StringBuilder (using System.Text;), called as sb.Insert(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. The string, char, and number overloads exist in every .NET version. Insert(int, ReadOnlySpan<char>) was added in .NET Core 2.1 and later.

Syntax

Pass the position first, then the value. A string overload can also repeat the text.

C#
StringBuilder sb = new StringBuilder("Hello World");
sb.Insert(5, ",");        // Hello, World
sb.Insert(0, '>');        // >Hello, World
sb.Insert(1, "-", 2);     // >--Hello, World
CallWhat it does
sb.Insert(0, "text")Adds text at the start; the old text moves right.
sb.Insert(sb.Length, "!")Adds at the end, like Append("!").
sb.Insert(2, "ab", 3)Inserts "ab" three times in a row: ababab.
sb.Insert(1, 42)Numbers and bools are converted to text first, like Append does.

The index must be from 0 to Length (inclusive). Anything outside that range throws ArgumentOutOfRangeException. Every insert near the front has to move all the characters after it, so many inserts at position 0 are slower than appending and reversing once.

Library declarations
C#
public StringBuilder Insert(int index, string? value);
public StringBuilder Insert(int index, string? value, int count);
public StringBuilder Insert(int index, char value);
public StringBuilder Insert(int index, char[]? value, int startIndex, int charCount);
public StringBuilder Insert(int index, int value);  // also bool, long, double, object, ...
public StringBuilder Insert(int index, ReadOnlySpan<char> value); // .NET Core 2.1+

Parameters

ParameterDescription
indexRequired. The position where the new text starts. 0 is the front, Length is the end.
valueThe text or value to put in. Non-text values are turned into text. A null string inserts nothing.
countOptional, with a string. How many times to insert the text. Must be 0 or more.

Return Value

Return ValueDescription
StringBuilderThe same builder, with the value inserted and the following characters shifted right.

C# Examples

Example 1 — Insert at the Front, Middle, and End

Put a comma inside a phrase, a prefix at position 0, and a ! at the very end.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("Hello World");

        sb.Insert(5, ",");
        Console.WriteLine(sb.ToString());

        sb.Insert(0, ">> ");
        Console.WriteLine(sb.ToString());

        sb.Insert(sb.Length, '!');
        Console.WriteLine(sb.ToString());
    }
}

How It Works

  1. Middle. Position 5 is the space after Hello. Inserting "," there pushes the space and World one place right.
  2. Front. Insert(0, ">> ") places three characters before everything else, and the whole phrase shifts by 3.
  3. End. sb.Length is the position just past the last character, so inserting there works exactly like appending.
  4. One builder throughout. All three edits change the same object; no new string is made until ToString() is called for printing.

Example 2 — Insert Numbers, Booleans, and Repeated Text

Insert an int and a char in a chain, repeat a short string, and drop a bool into brackets.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("items");
        sb.Insert(0, 3).Insert(1, ' ');
        Console.WriteLine(sb.ToString());

        StringBuilder line = new StringBuilder("[]");
        line.Insert(1, "ab", 3);
        Console.WriteLine(line.ToString());

        line.Insert(1, true);
        Console.WriteLine(line.ToString());
    }
}

How It Works

  1. Number to text. Insert(0, 3) writes the digit 3 at the front, giving 3items.
  2. Chain a second insert. The returned builder takes .Insert(1, ' '), which slips a space between the digit and the word.
  3. Repeat count. Insert(1, "ab", 3) puts ab three times between the brackets, so [] becomes [ababab].
  4. A bool. true is written as True at position 1, right after the opening bracket, and the earlier letters move right.

Example 3 — Pad With Zeros and Add Thousands Separators

Use Insert in loops to pad a number on the left and to put commas into a long number, then try an index past the end.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder num = new StringBuilder("42");
        while (num.Length < 5) {
            num.Insert(0, '0');
        }
        Console.WriteLine(num.ToString());

        StringBuilder digits = new StringBuilder("1234567");
        for (int i = digits.Length - 3; i > 0; i -= 3) {
            digits.Insert(i, ',');
        }
        Console.WriteLine(digits.ToString());

        try {
            num.Insert(10, "x");
        } catch (ArgumentOutOfRangeException) {
            Console.WriteLine("index past the end");
        }
    }
}

How It Works

  1. Pad on the left. The loop inserts '0' at the front until the length reaches 5, so 42 becomes 00042.
  2. Work from the right. The first comma goes at position 4 (three digits from the end), then at position 1. Going right to left means the earlier comma never shifts the spot for the next one.
  3. Result. 1234567 turns into 1,234,567. For real number formatting, ToString("N0", CultureInfo.InvariantCulture) does this for you.
  4. Out of range. num has 5 characters, so position 10 does not exist and Insert throws ArgumentOutOfRangeException.

Frequently Asked Questions

It puts a value into the text at a given position and moves the following characters to the right. It changes the builder and returns the same builder.
Any index from 0 to Length, inclusive. 0 inserts at the front and Length inserts at the end. Other values throw ArgumentOutOfRangeException.
string.Insert returns a new string and leaves the original unchanged. StringBuilder.Insert changes the builder itself.
Yes. Insert(index, text, count) inserts the text count times in a row, for example Insert(0, "-", 3) adds three dashes.
Each insert has to shift all the characters after the position. A few inserts are fine; for thousands of front inserts, build the text in another order instead.

Next: Remove()

Cut a range of characters out of the builder by start position and length.

Remove() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful