C# String Insert() Method Last Updated : 23 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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# // 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); } } OutputGiven String: GeeksGeeks After Inserting : GeeksforGeeks After Inserting : Geeks Geeks Syntax of String Insert() Methodpublic 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# // 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")); } } OutputCurrent 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). C# // 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)); } } OutputCurrent 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. Comment More infoAdvertise with us Next Article C# String Insert() Method M Mithun Kumar Follow Improve Article Tags : Misc C# CSharp-method CSharp-string Practice Tags : Misc Similar Reads std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou 4 min read C# String CopyTo() Method In C#, the CopyTo() is a method of String Class. It is used to copy a specified number of characters from a specified position in the string and it copies the characters of this string into an array of Unicode characters.Example 1: Using the CopyTo() method to copy characters from a string to a char 2 min read Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i 5 min read std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read std::string::append() in C++ The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C 3 min read std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign 5 min read map insert() in C++ STL The std::map::insert() is a built-in function of C++ STL map container which is used to insert new elements into the map. In this article, we will learn how to use map::insert() function in our C++ programs.Example:C++// C++ program to illustrate how to use // map::insert #include <bits/stdc++.h 5 min read StringBuffer insert() in Java The StringBuffer.insert() method in Java allows us to insert a string representation of a given data type at a specified position in a StringBuffer. This method is useful when we need to modify a string at specific positions without creating a new string each time by making it more efficient than co 4 min read Vector insert() in C++ STL In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function:C++#include <bits/stdc++.h> using 4 min read set::insert() function in C++ STL The std::set::insert() is a built-in function of C++ STL set container which is used to insert new elements in it. In this article, we will learn how to use set::insert() function in our C++ programs.SyntaxThe string::replace() function provides 6 different overloads for different purposes:st.insert 4 min read Like