Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Insert() and String.Remove() methods

Reference for C# string.Insert() and string.Remove(): create modified copies of a string by position, with ranges, index errors and StringBuilder alternatives.

Strings in C# cannot be changed once created, so there are no methods that edit a string in place. Insert and Remove return a new string with text added at a position or a range of characters taken out. The original stays as it was.

Both work by index, not by searching. To change text you find by value, use Replace. To pick a piece out of the middle, use Substring or a range like s[2..5].

C#
"abcdef".Insert(3, "-");  // "abc-def"
"abcdef".Remove(2, 2);    // "abef"

Try it

C# Insert and Remove

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Errors, ranges and alternatives

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public string Insert(int startIndex, string value)
public string Remove(int startIndex)
public string Remove(int startIndex, int count)
Name Description
startIndex Zero-based position where insertion happens, or where removal starts.
value The text to insert. Must not be null.
count How many characters to remove.

Returns: a new string. Insert returns a string whose length is the original plus value.Length. Remove(startIndex) returns the part before startIndex.

Errors / undefined behavior: Insert throws ArgumentOutOfRangeException if startIndex is negative or greater than the length (equal to the length is allowed). Remove throws it if startIndex or count is negative, or startIndex + count is larger than the length. Insert throws ArgumentNullException for a null value.

Usage notes

  • Immutable. Forgetting to capture the result is the standard mistake: s.Insert(1, "x"); on its own does nothing.
  • Indexes shift. After inserting or removing, every later position moves. When doing several edits, work from the end toward the start (as in the digit grouping example) so earlier indexes stay valid.
  • Cost. Each call copies the entire string, so doing it in a loop on large text is quadratic. StringBuilder has Insert, Remove and Replace that edit a buffer.
  • UTF-16 positions. Indexes count char values. Do not split in the middle of a surrogate pair or a combined character, or you will corrupt the text. See Iterating characters.
  • Not a search. To delete a known substring wherever it is, use Replace(text, ""). To remove white space at the ends, use Trim.
  • Slicing alternative. Ranges (s[..i] + x + s[i..]) express the same thing as Insert and compile to similar code. Choose whichever reads better.
  • Spans. For allocation-free manipulation, work on Span<char> buffers; see spans and memory.