C# String Insert() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, Insert() puts a piece of text into a string at the position you choose. Everything that was at that position and after it moves to the right. The result is a new string.

The original string does not change. That surprises many beginners: word.Insert(5, ",") on its own line does nothing useful, because the new string is thrown away. Write word = word.Insert(5, ","); or store the result in a new variable.

This is an instance member of System.String. Call it on a string variable, such as text.Insert(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Insert() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.

Syntax

Pass the position first and the text second.

C#
string word = "Hello World";
string result = word.Insert(5, ",");   // "Hello, World"

// word is still "Hello World"
CallWhat it does
text.Insert(0, ">> ")Add text at the very start.
text.Insert(5, ",")Add text before the character that is now at index 5.
text.Insert(text.Length, "!")Add text at the very end (same as text + "!").
text = text.Insert(2, "-")Keep the result by assigning it back.

startIndex may be any value from 0 to Length. A negative value or a value larger than Length throws ArgumentOutOfRangeException. A null value throws ArgumentNullException. Inserting an empty string gives back the same text.

Library declarations
C#
public string Insert(int startIndex, string value);

Parameters

ParameterDescription
startIndexRequired. The position where the new text goes. Allowed range: 0 to Length.
valueRequired. The text to insert. Must not be null.

Return Value

Return ValueDescription
stringA new string with value placed at startIndex. Its length is Length + value.Length.

C# Examples

Example 1 — Add a Comma and Keep Both Strings

Insert a comma after Hello, then print both the old and the new string to see that the old one did not change.

C#
using System;

class Program {
    static void Main() {
        string word = "Hello World";
        string result = word.Insert(5, ",");

        Console.WriteLine(word);
        Console.WriteLine(result);
        Console.WriteLine(result.Length);
    }
}

How It Works

  1. Pick the spot. Hello fills indexes 0 to 4, so index 5 is the space. The comma goes in front of it.
  2. A brand new string. result holds Hello, World, and word still holds the old text.
  3. One character longer. The old string has 11 characters and the comma adds 1, so Length is 12.
  4. Why two variables help. Printing both proves that Insert builds a copy; the original string object is never touched.

Example 2 — Insert at the Start, at the End, and Too Far

Try the two edge positions that are allowed, and one position that is not.

C#
using System;

class Program {
    static void Main() {
        string code = "1234";

        Console.WriteLine(code.Insert(0, "#"));
        Console.WriteLine(code.Insert(code.Length, "!"));

        try {
            Console.WriteLine(code.Insert(10, "x"));
        } catch (ArgumentOutOfRangeException ex) {
            Console.WriteLine(ex.GetType().Name);
        }
    }
}

How It Works

  1. Index 0 is the front. The # lands before the first digit.
  2. Index Length is the back. code.Length is 4, which is one past the last index, and it is still allowed. The ! is added at the end.
  3. Past the end is an error. Index 10 is beyond the string, so Insert throws instead of padding with spaces.
  4. Stable output. The catch block prints only the exception's type name, not its message, so the output is the same on every .NET version.

Example 3 — Turn 20261118 into 2026-11-18

Insert two dashes into a compact date. Insert the later one first so the positions stay correct.

C#
using System;

class Program {
    static void Main() {
        string raw = "20261118";

        string date = raw.Insert(6, "-").Insert(4, "-");
        Console.WriteLine(date);

        string phone = "5551234".Insert(3, "-");
        Console.WriteLine(phone);
        Console.WriteLine(raw);
    }
}

How It Works

  1. Right to left. The first call puts a dash at 6, giving 202611-18. Index 4 has not moved, because the change happened after it.
  2. Then the left dash. .Insert(4, "-") runs on the result of the first call and gives 2026-11-18. Calling it in the other order would put the second dash in the wrong place.
  3. Chaining. Each Insert returns a string, so you can call another method on it right away.
  4. Raw stays raw. The last line prints 20261118, proving that neither call edited the original variable.

Frequently Asked Questions

It returns a new string with your text placed at the position you give. The characters at and after that position move to the right. The original string is not changed.
C# strings are immutable. Insert returns a new string. Assign it: text = text.Insert(3, "x");
Any value from 0 to text.Length. 0 inserts at the start and Length inserts at the end. Anything else throws ArgumentOutOfRangeException.
Insert at the highest position first and work toward the start, so earlier inserts do not shift the later positions. For many edits, StringBuilder.Insert avoids creating lots of temporary strings.
Remove(startIndex, count) deletes characters and also returns a new string.

Next: Intern()

Get the single shared copy of a string from the runtime's string pool, adding it if needed.

Intern() 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.

9 people found this page helpful