programming-examples/c-sharp/Strings/C# Sharp to insert a substring before the first occurrence of a string.cs
2019-11-15 12:59:38 +01:00

24 lines
943 B
C#

using System;
public class Exercise20
{
public static void Main()
{
string str1;
string findstring;
string insertstring;
int i;
Console.Write("\n\nInsert a substing before the first occurence of a string :\n");
Console.Write("--------------------------------------------------------------\n");
Console.Write("Input the original string : ");
str1 = Console.ReadLine();
Console.Write("Input the string to be searched for : ");
findstring = Console.ReadLine();
Console.Write("Input the string to be inserted : ");
insertstring = Console.ReadLine();
i=str1.IndexOf(findstring); // locate the position of the first occurence of the string
insertstring = " " + insertstring.Trim() + " ";
str1 = str1.Insert(i, insertstring);
Console.Write("The modified string is : {0}\n\n",str1);
}
}