The Wayback Machine - https://web.archive.org/web/20240827150431/https://www.geeksforgeeks.org/c-sharp-insert-method/
Open In App

C# | Insert() Method

Last Updated : 31 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C#, Insert() method is a String method. It is used to return a new string in which a specified string is inserted at a specified index position in the current string instance.

Syntax:

public string Insert(int Indexvalue, string value)

Explanation:

  • Indexvalue: It is the index position of 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.

Exceptions:

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

Note: This method always returns a new string which is modified with value inserted at the specified position. The return value type of Insert() method is System.String. If Indexvalue is equal to the length of the current instance, then the value is appended to the end of this instance.

Example:

Input : str  = "GeeksForGeeks"
        str.Insert(5, "GFG");
Output: GeeksGFGForGeeks

Input : str  = "GeeksForGeeks"
        str.Insert(8, " ");
Output: GeeksFor Geeks

Below are the programs to illustrate the Insert() Method :

  • Program 1:




    // C# program to demonstrate the 
    // Insert method
    using System;
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // string
            String str = "GeeksForGeeks";
      
            Console.WriteLine("Current string: " + str);
      
            // insert GFG at index 5 where string is append
            Console.WriteLine("New string: " + str.Insert(5, "GFG"));
        }
    }

    
    

    Output:

    Current string: GeeksForGeeks
    New string: GeeksGFGForGeeks
    
  • Program 2:




    // C# program to demonstrate the 
    // Insert method
    using System;
    class Geeks {
      
        // Main Method 
        public static void Main()
        {
      
            // string
            String str = "GeeksForGeeks";
      
            Console.WriteLine("Current string: " + str);
      
            // insert space at index 8 where string is append
            Console.WriteLine("New string: " + str.Insert(8, " "));
        }
    }

    
    

    Output:

    Current string: GeeksForGeeks
    New string: GeeksFor Geeks
    
  • Program 3:




    // C# program to demonstrate the 
    // Insert method
    using System;
    class Geeks {
      
        // Main Method
        public static void Main()
        {
            Console.WriteLine("Hey Started".Insert(3, " ProGeek2.0"));
        }
    }

    
    

    Output:

    Hey ProGeek2.0 Started
    

Reference: https://msdn.microsoft.com/en-us/library/system.string.insert



Similar Reads

C# | Insert an element into the ArrayList at the specified index
ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayList at the specified index. Properties of ArrayList
3 min read
C# | Insert an object at the top of the Stack - Push Operation
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Push(T) Method is used to inserts an object at the top of the Stack<T>. Properties: The
2 min read
C# | How to insert the elements of a collection into the List at the specified index
List<T>.InsertRange(Int32, IEnumerable<T>) Method is used to insert the elements of a collection into the List<T> at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows dupli
3 min read
C# | Insert at the specified index in StringCollection
StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.Insert(Int32, String) method is used to insert a string into the StringCollection at the specified index. Syntax: public void Insert
2 min read
C# | Insert into OrderedDictionary with key and value at specified index
OrderedDictionary.Insert(Int32, Object, Object) method is used to insert a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index : It is the zero-based index at which the element should be inserted. key : It is the
3 min read
C# | Insert an element into Collection<T> at specified index
Collection<T>.Insert(Int32, T) method is used to insert an element into the Collection<T> at the specified index. Syntax: public void Insert (int index, T item); Parameters: index : The zero-based index at which item should be inserted. item : The object to insert. The value can be null for reference types. Exception: This method will g
3 min read
C# | Insert a new entry in OrderedDictionary with specified key and value
OrderedDictionary.Insert(Int32, Object, Object) Method is used to inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index: It is the zero-based index of type System.Int32 at which the element should be inser
3 min read
C# | How to insert an element in an Array?
An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C#. Let's say we have an array and we want to insert an element at a specific position in this array. Here's how to do it. First get the element to be inserted, say x Then get the position at which this eleme
2 min read
Difference between Method Overriding and Method Hiding in C#
Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding. In simple words, Overriding is a feature that allows a subclass or child class to provide a specific im
4 min read
C# | Uri.MakeRelativeUri(Uri) Method
Uri.MakeRelativeUri(Uri) Method is used to determine the difference between two Uri instances. Syntax: public Uri MakeRelativeUri (Uri uri); Here, it takes the URI to compare to the current URI. Return Value: If the hostname and scheme of this URI instance and uri are the same, then this method returns a relative Uri that, when appended to the curr
3 min read
Array.GetValue() Method in C# with Examples | Set - 1
Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetValue(Int32, Int32, Int32) Array.GetValue(Int64, Int
4 min read
C# | CharEnumerator.MoveNext() Method
CharEnumerator.MoveNext() Method is used to increments the internal index of the current CharEnumerator object to the next character of the enumerated string. Syntax: public bool MoveNext(); Return Value: This method returns the boolean value true value if the index is successfully incremented and within the enumerated string otherwise, false. Belo
2 min read
C# | Math.IEEERemainder() Method
In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number. Syntax: public static double IEEERemainder (double a, double b); Parameters: a: It is the dividend of type System.Double.b: It is the divisor of type System.Double. Return Type: This meth
2 min read
C# | Substring() Method
In C#, Substring() is a string method. It is used to retrieve a substring from the current instance of the string. This method can be overloaded by passing the different number of parameters to it as follows: String.Substring(Int32) Method String.Substring(Int32, Int32) Method String.Substring Method (startIndex) This method is used to retrieves a
3 min read
C# Program to Demonstrate the Use of Exit() Method of Environment Classhttps://origin.geeksforgeeks.org/?p=705454
Environment Class provides information about the current platform and manipulates, the current platform. The Environment class is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings informatio
3 min read
C# | IndexOfAny() Method
In C#, IndexOfAny() method is a String Method. It is used to search the specified character index present in the string which will be the first occurrence from start index. It returns index as an integer value. This method can be overloaded by changing the number of arguments passed to it. IndexOfAny Method (Char[])IndexOfAny Method (Char[], Int32)
7 min read
C# | CharEnumerator.GetType() Method
CharEnumerator.GetType() Method is used to get the type of the current instance. This method is inherited from the Object Class. Syntax: public Type GetType(); Return Value: This method returns the exact runtime type of the current instance. Below are the programs to illustrate the use of CharEnumerator.GetType() Method:+ Example 1: // C# program t
2 min read
File.GetLastWriteTimeUtc() Method in C# with Examples
File.GetLastWriteTimeUtc(String) is an inbuilt File class method which is used to return the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.Syntax: public static DateTime GetLastWriteTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: Thi
2 min read
MathF.Sin() Method in C# with Examples
MathF.Sin(Single) is an inbuilt MathF class method which returns the sine of a given float value argument(specified angle). Syntax: public static float Sin (float x); Here, x is the angle(measured in radian) whose sine is to be returned and the type of this parameter is System.Single. Return Value: This method will return the sine of x of type Syst
2 min read
Double.CompareTo Method in C# with Examples
Double.CompareTo Method is used to compare the current instance to a specified object or Double object. It will return an integer which shows whether the value of the current instance is greater than, less than or equal to the value of the specified object or Double object. There are 2 methods in the overload list of this method as follows: Compare
5 min read
C# | Graphics.DrawLine() Method | Set - 1
Graphics.DrawLine() Method is used to draw a line connecting the two points specified by the coordinate pairs. There are 4 methods in the overload list of this method as follows: DrawLine(Pen, PointF, PointF) Method DrawLine(Pen, Int32, Int32, Int32, Int32) Method DrawLine(Pen, Single, Single, Single, Single) Method DrawLine(Pen, Point, Point) Meth
4 min read
UInt16.GetHashCode Method in C# with Examples
UInt16.GetHashCode method is used to get the HashCode for the current UInt16 instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of the above discussed-method: Example 1: // C# program to illustrate the // UInt16.GetHashCode() Method using Syste
1 min read
Stack.Peek Method in C#
This method(comes under System.Collections namespace) is used to return the object at the top of the Stack without removing it. This method is similar to the Pop method, but Peek does not modify the Stack. Syntax: public virtual object Peek (); Return Value: It returns the Object at the top of the Stack. Exception: Calling Peek() method on empty st
2 min read
C# | IsNullOrEmpty() Method
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned "" or String.Empty (A constant for empty strings). Syntax: public static bool IsNullOrEmpty(String str) Explanation: This method w
2 min read
C# | Math.Sign() Method
In C#, Sign() is a math class method which returns an integer that specify the sign of the number. This method can be overloaded by changing the data type of the passed arguments as follows: Math.Sign(Decimal): Returns the integer that specifies the sign of a decimal number. Math.Sign(Double): Returns the integer that specifies the sign of a double
3 min read
Int64.CompareTo Method in C# with Examples
Int64.CompareTo Method is used to compare the current instance to a specified object or Int64 and returns a sign of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Int64) Method CompareTo(Object) Method Int64.CompareTo(Int64) Method This method is used to compare the current instance to a specifi
4 min read
C# | PadRight() Method
In C#, PadRight() is a string method. This method is used to left-aligns the characters in String by padding them with spaces or specified character on the right, for a specified total length. This method can be overloaded by passing different parameters to it. String.PadRight Method(Int32)String.PadRight Method(Int32, Char)String.PadRight Method(I
4 min read
C# | EndsWith() Method
In C#, EndsWith() is a string method. This method is used to check whether the ending of the current string instance matches with a specified string or not. If it matches, then it returns the string otherwise false. Using "foreach" loop, it is possible to check many strings. This method can be overloaded by passing different type and number of argu
5 min read
C# | Char.CompareTo() Method
In C#, Char.CompareTo() is a System.Char struct method which is used to compare this instance of a specified object or value type and check whether the given instance is precedes, follow, or appears in the same position in the sort order as the specified object or value type. This method can be overloaded by passing the different type of arguments
3 min read
C# | Char.IsUpper() Method
In C#, Char.IsUpper() is a System.Char struct method which is used to check whether a Unicode character can be categorized as an uppercase letter or not. Valid uppercase letters will be the members of the UnicodeCategory: UppercaseLetter. This method can be overloaded by passing different type and number of arguments to it. Char.IsUpper(Char) Metho
3 min read
Article Tags :