Inserting Word in Between the String - C#

This is my first post and I have given an example using C# which gets a string from user and inserts anywhere in-between the existing string.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

 

            string data = "This is my article";

            Console.WriteLine(data);            //displays data

 

            Console.WriteLine("\n Which word you want to insert into the above string?");

            string udata = Console.ReadLine();    //gets the string from user

 

            Console.WriteLine("\n After which word you need to insert string?");

            string udata1 = Console.ReadLine(); //gets the string next to which new string has to be inserted

 

            int position = data.IndexOf(udata1);

            string complete = data.Insert(position + udata1.Length, udata);

            Console.WriteLine(complete);

 

            Console.ReadKey();

        }

    }

}

1.png



Please do let me know if you don't understand anything in the code. 

Thank you. 
 

Thanks to C-Sharp Corner for letting me to share my ideas.


Ebook Download
View all
Learn
View all