Insert() Method in C#


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 −

 Live Demo

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

 Live Demo

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
raja
Published on 03-Dec-2019 09:14:00
Advertisements