
Insert places one string into another. This forms a new string in your C# program. Here we see how you can use the string Insert method to place one string in the middle of another one—or at any other position.
Key point: You can insert one string at any index into another.

Here we see two examples of the Insert method on string instances. In your programs, you will have strings that are dynamic and not string literals, but you will often need to insert literals into them. Part A in the example inserts at an arbitrary index, while part B inserts in a relative position to another substring.
These C# examples use the Insert method on the string type. A benchmark is provided.
Program that inserts strings [C#]
using System;
class Program
{
static void Main()
{
//
// A. You can insert one string between two others.
//
string names = "Romeo Juliet";
string shakespeare = names.Insert(6, "and ");
Console.WriteLine(shakespeare);
//
// B. You can insert a string right after another string.
//
string names2 = "The Taming of Shrew";
int index2 = names2.IndexOf("of ");
string shakespeare2 = names2.Insert(index2 + "of ".Length, "the ");
Console.WriteLine(shakespeare2);
}
}
Output
Romeo and Juliet
The Taming of the ShrewDetails on part A. This section Inserts the string "and " into the sixth index of the original string. When you count indexes, you designate the first character as 0. Then, you count up from zero.
Details on part B. Here we see how you can Insert a string into a position relative to another substring. This code will insert the word "the " right after the word "of ". This is useful in fixing up poorly formatted strings in your C# programs.

The Insert method goes directly to an internal, unmanaged code implementation that Microsoft doesn't make public. This means it is highly optimized and avoids some overhead of you doing it in your own code.
Note: On invalid arguments, the Insert method raises ArgumentNullExceptions and ArgumentOutOfRangeExceptions. This means you must not send it negative indexes, a null string, or an index that is not present in the current string.

In collections in the C# language, you can insert elements to the start or end. With the string Insert method, this works correctly, but is much slower than using string concatenation, as with + or string.Concat.
When you need to prepend or append. My findings here are that you shouldn't use Insert at index 0 or at the last index. Instead, use + for concatenation. If your string is very long, consider StringBuilder. Results were 1637 ms for Insert, 652 ms for Concat.
Input strings used [C#]
string a = 10000.ToString();
string b = 20000.ToString();
Code benchmarked in loop [C#]
(10000000 iterations were tested.)
// Insert code
string c = a.Insert(0, b);
string d = c.Insert(c.Length, b);
// Concat code
string c = b + a;
string d = c + b;
Results
Insert start or end: 1637 ms
Concat strings to start or end: 652 ms [faster]
In this tutorial, we saw how you can use the Insert method to "inject" a string into the middle of two others, using the C# programming language. You should only use this when you need to Insert somewhere between and not including the first and last index. If you need to prepend or append a string, use the plus + operator.
String Type