Home » C#.Net

String.Insert() method with example in C#

C# String.Insert() method: Here, we are going to learn about the Insert() method of String class with example.
Submitted by IncludeHelp, on September 21, 2019

C# String.Insert() Method

String.Insert() method is used to insert a string in an existence string at specified index and returns a new string.

Syntax:

    public string String.Insert(int index, string value);

The method is called with "this" string i.e. the string in which we have to insert the string.

Parameter(s):

  • index – represents the index/position of the current string where new string value to be inserted.
  • value – a new string to be inserted.

Return value:

  • string – it returns a new string containing the inserted string at given index.

Example:

    Input:
    string str = "IncludeHelp";
    string str1 = " programming ";
    
    Function call
    str.Insert(7, str1);

    Output:
    Include programming Help

C# Example to convert string to characters array using String.Insert() method

Example 1:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string str = "IncludeHelp";

        // inserting space between Include and Help
        string new_string = str.Insert(7, " ");

        Console.WriteLine("str: " + str);
        Console.WriteLine("new_string: " + new_string);
    }
}

Output

str: IncludeHelp
new_string: Include Help

Example 2:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string str = "IncludeHelp";
        string str1 = " programming ";

        // inserting str1 after "Include"
        string new_string = str.Insert(7, str1);

        Console.WriteLine("str: " + str);
        Console.WriteLine("new_string: " + new_string);

    }
}

Output

str: IncludeHelp
new_string: Include programming Help





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.