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 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::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 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 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 C# String Class In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is represented by a class System.String. The String c 9 min read Practice questions on Strings String is an important topic from GATE exam point of view. We will discuss key points on strings as well different types of questions based on that. There are two ways to store strings as character array (char p[20]) or a pointer pointing to a string (char* s = âstringâ), both of which can be access 4 min read IntBuffer put() methods in Java | Set 1 put(int i) The put(int i) method of java.nio.IntBuffer Class is used to write the given int into the newly created int buffer at the current position, and then increments the position. Syntax : public abstract IntBuffer put(int i) Parameters: This method takes the int value i as a parameter which is 6 min read snprintf() in C In C, snprintf() function is a standard library function that is used to print the specified string till a specified length in the specified format. It is defined in the <stdio.h> header file.In this article, we will learn about snprintf() function in C and how to use it in our program.SyntaxC 3 min read Like