TheDeveloperBlog.com

Home | Contact Us

C# Insert String Method

These C# examples use the Insert string method. A benchmark of Insert performance is provided.

Insert places one string into another. This forms a new string in your C# program.

We use the string Insert method to place one string in the middle of another one—or at any other position.

Tip: We can insert one string at any index into another. IndexOf can return a suitable index.

Tip 2: Insert can be used to concatenate strings. But this is less efficient—concat, as with + is faster.

Example. 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.

String Literal

Note: Part "A" of the example inserts at an arbitrary index. Part "B" inserts in a relative position to another substring.

Based on:

.NET 4.5

C# program that inserts strings

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 Shrew

In part A, we insert the specified string into the sixth index of the original string. When you count indexes, you designate the first character as 0. And then you count up from zero.

Part B inserts 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.

Internals. 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.

So: We must not send Insert() negative indexes, a null string, or an index that is not present in the current string.

Benchmark. 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.

Results: My findings here are that you should not 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]

Summary. With Insert, we inserted a string into the middle of an existing string. Insert is useful here. But if we need to prepend or append a string, use the plus + operator—it is faster.