C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
String: We start with a string that reads "bird, frog." At index 6, we insert a new substring with the value of "green."
So: Our result string contains the value "bird, green frog." We can still use all the original strings in the program if we need them.
C# program that uses Insert
using System;
class Program
{
static void Main()
{
string value = "bird, frog";
// Insert string at index 6.
string adjusted = value.Insert(6, "green ");
Console.WriteLine("FINAL RESULT: " + adjusted);
}
}
Output
FINAL RESULT: bird, green frog
Here: We find the position of the comma in the string with IndexOf, and then Insert() a new string 2 characters after it.
C# program that uses Insert, IndexOf
using System;
class Program
{
static void Main()
{
string value = "cat, frog";
// Find the comma.
int position = value.IndexOf(", ");
// Make sure we have a comma.
if (position >= 0)
{
// Insert a string 2 places after the comma.
string adjusted = value.Insert(position + 2, "green ");
Console.WriteLine("END: " + adjusted);
}
}
}
Output
END: cat, green frog
C# program that causes null error
class Program
{
static void Main()
{
string value = "test";
string result = value.Insert(0, null);
}
}
Output
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.String.Insert(Int32 startIndex, String value)
at Program.Main()....
Here: We benchmark the Insert method with an index of 0, against a string "prepend" operation with the plus operator.
Version 1: This calls Insert to insert 2 strings at index 0. It makes sure the result is correct.
Version 2: This uses string.Concat (the plus operator) to prepend strings. It also ensures it comes up with the correct result.
Result: Using Insert at index 0 is a better choice, but the performance difference is small.
Tip: There is probably no performance reason to prefer Insert over concat. But insert() might be clearer for this use.
C# program that benchmarks Insert, string.Concat
using System;
using System.Diagnostics;
class Program
{
const int _max = 10000000;
static void Main()
{
string beforePart = 5.ToString();
string afterPart = 10000.ToString();
// Version 1: use Insert at position 0.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string result = afterPart.Insert(0, beforePart);
result = result.Insert(0, "X");
if (result != "X510000")
{
break;
}
}
s1.Stop();
// Version 2: use concat to add new prefix.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string result = beforePart + afterPart;
result = "X" + result;
if (result != "X510000")
{
break;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
47.61 ns Insert(0)
50.30 ns + (Concat)
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.