find and solve || findandsolve.com
Please wait.....

C# String Insert()

In the C# Insert() method is used to a new string in which a specified string is inserted at a specified index. Or represents the index/position of the current string where new string value to be inserted.

Signature

public string Insert(Int32 first, String second)

first: It is the index position of current string where the new value will be inserted. The type of this parameter is System.Int32.

second: The String value to be inserted the type of this parameter is System.String.

Exceptions

  • ArgumentNullException: If the String value is null.
  • ArgumentOutOfRangeException: If Indexvalue is negative or greater than the length of the string.

Return

It returns a new modified string.

C# String Insert() Method Example

using System;
public class Program
{
  public static void Main()
    {
         string str = "Hello FindAndSolve";
          string str1 = str.Insert(5,"!");
          Console.WriteLine(str1);
    }
}

Output

Hello! FindAndSolve


Another Example

using System;
public class Program
{
  public static void Main()
    {
        // string
        String str = "Hello FindAndSolve";
        Console.WriteLine("Current string: " + str);
        // insert space at index 7 where string is append
        Console.WriteLine("New string: " + str.Insert(7, " "));
    }
}

Output

Current string: Hello FindAndSolve

New string: Hello F indAndSolve


Another Example

using System;
public class Program
{
  public static void Main()
    {
      Console.WriteLine("Hello Started".Insert(3, " FindAndSolve"));
    }
}

Output

Hel FindAndSolvelo Started