C# String Insert() Method
The Insert() method in C# is a part of the String class. It is used to insert a new string at a specified index position and return a new string with the inserted string value. This method does not make the changes in the original string because strings are immutable in C#. It returns a new modified string.
Now, let us go through a simple example to understand the usage of this method.
Example 1: Use of the Insert() method to add a new string at a specified index position.
// C# program to illustrate the
// method string Insert(int startIndex, string value)
using System;
class Geeks
{
public static void Main()
{
// original string
String s = "GeeksGeeks";
Console.WriteLine("Given String: " + s);
// Insert "for" at index 5
String res = s.Insert(5, "for");
// New string after insertion
Console.WriteLine("After Inserting : " + res);
// Insert " " (space) at index 8
res = s.Insert(5, " ");
Console.WriteLine("After Inserting : " + res);
}
}
Output
Given String: GeeksGeeks After Inserting : GeeksforGeeks After Inserting : Geeks Geeks
Syntax of String Insert() Method
public string Insert(int Indexvalue, string value)
Parameters:
- Indexvalue: It is the index position of the in current string where the new value will be inserted. The type of this parameter is System.Int32.
- value: The String value to be inserted. The type of this parameter is System.String.
Return Type: Returns a new modified string with a value inserted at the specified position. The return type is System.String.
Exceptions:
- ArgumentNullException: If the String value is null.
- ArgumentOutOfRangeException: If Indexvalue is negative or greater than the length of the string.
Note: String is immutable in C#. This methods cannot modify the original string instead it return a new string with the applied changes.
Example 2: Inserting a string in the specific index position using the Insert() method.
// C# program to demonstrate the
// Insert method
using System;
class Geeks
{
public static void Main()
{
// string declaration and initialization
String str = "Geeks";
// String before insert operation
Console.WriteLine("Current string: " + str);
// insert Geeks at index 5 where string is append
Console.WriteLine("New string: " + str.Insert(5, "forGeeks"));
}
}
Output
Current string: Geeks New string: GeeksforGeeks
Example 3: Inserting the country code at the starting of the number provided by user, using Insert() method (Real-World use case).
// Using the Insert() method to
// add the country code in strating of
// number provided by user
using System;
class Geeks
{
public static void Main()
{
// string declaration and initialization
String num = "357-xxx-xxxx";
// String before insert operation
Console.WriteLine("Current string: " + num);
// Country code of india to be inserted
String code = "+91-";
// insert india's contry code at
// index 0 where string is append
Console.WriteLine("New string: " + num.Insert(0, code));
}
}
Output
Current string: 357-xxx-xxxx New string: +91-357-xxx-xxxx
Explanation: In the above example, we use Insert() method to add contry code in the starting of a number. This method is very usefull for inserting different values in a specific position of a string.