The Insert() method in C# is used to return a new string in which a specified string is inserted at a specified index position in this instance.
Syntax
The syntax is as follows −
public string Insert (int begnIndex, string val);
Above, the parameter begnIndex is the zero-based index position of the insertion, whereas val is the string to insert.
Example
Let us now see an example −
using System;
public class Demo{
public static void Main(){
String str = "JohnWick";
Console.WriteLine("Initial string = "+str);
String res = str.Insert(5, " ");
Console.WriteLine("Updated = "+res);
}
}Output
This will produce the following output −
Initial string = JohnWick Updated = JohnW ick
Example
Let us now see another example
using System;
public class Demo{
public static void Main(){
String str = "WelcomeJacob";
Console.WriteLine("Initial string = "+str);
String res = str.Insert(7, " here, ");
Console.WriteLine("Updated = "+res);
}
}Output
This will produce the following output −
Initial string = WelcomeJacob Updated = Welcome here, Jacob