C# String - Insert() Method



The C# String Insert() method is used to return a new string in which a specified string is inserted at a given index position in this instance of string.

Exception

There are a few exceptions to this method −

  • ArgumentNullException: When the value is null.
  • ArgumentOutOfRangeException: When startIndex is negative or greater than the length of the string object.

Syntax

Following is the syntax of the C# string Insert() method −

public string Insert(int startIndex, string value);

Parameters

This method accepts the following parameters −

  • startIndex: It represents the index position of current string where the new value will be inserted.
  • value: It represents a string to be inserted at the specified position.

Return value

This method returns a new string. That is equal to the current string object. But the string is modified with a value inserted at the specified position.

Example 1: Insert Space Character

The following example inserts a space character in the fourth character position (i.e. the character at index 3) of a string using the Insert() method −

    
using System;
public class Program {
   public static void Main()  {
      String original_str = "tutorialspoint";
      Console.WriteLine("The original string: '{0}'", original_str);
      // modify the string...
      String modified_str = original_str.Insert(3, " ");
      Console.WriteLine("The modified string: '{0}'", modified_str);
   }
}

Output

Following is the output −

The original string: 'tutorialspoint'
The modified string: 'tut orialspoint'

Example 2: Insert a String

Let us look at another example of the Insert() method. Here, we define a string, and between the string's words, we are inserting another one −

using System;
public class Program {
   public static void Main() {
      String str = "Hello World!";
      String to_be_insert = "tutorialspoint";
      Console.WriteLine("The original string: '{0}'", str);
      // modify the string...
      String modified_str = str.Insert(3, to_be_insert);
      Console.WriteLine("The modified string: '{0}'", modified_str);
   }
}

Output

Following is the output −

The original string: 'Hello World!'
The modified string: 'Heltutorialspointlo World!'

Example 3: Insert at the Beginning of a String

In this example, we use the Insert() method to add a string at the beginning of the string, specifically at index position 0 −

using System;
class Program {
   static void Main() {
      string str = "World!";
      string modified_str = str.Insert(0, "Hello ");
      Console.WriteLine(modified_str);
   }
}

Output

Following is the output −

Hello World!

Example 4: Insert at the End of a String

In this example, we use the Insert() method to insert a string at the end of the current string object −

using System;
class Program {
   static void Main() {
      string str = "Hello";
      string to_be_insert = " tutorialspoint!";
      // modified the string
      string modified_str = str.Insert(str.Length, to_be_insert);
      Console.WriteLine(modified_str);
   }
}

Output

Following is the output −

Hello tutorialspoint!
csharp_strings.htm
Advertisements