Заменить в строке вхождения строки - C#
Формулировка задачи:
Даны три строки: S1, S2, S3. Заменить в строке S1 первое1|последнее2|все3 вхождения строки S2 на S3.
Решение задачи: «Заменить в строке вхождения строки»
textual
Листинг программы
string S1 = "God bless you (variants include God bless or bless you) is a common English expression, used to wish a person blessings in various situations, especially as a response to a sneeze, and also, when parting or writing a valediction.",
S2 = "bless",
S3 = "hate";
int choice = 1;
switch(choice)
{
case 1:
{
int IndexFirst = S1.IndexOf(S2);
S1 = S1.Remove(IndexFirst, S2.Length).Insert(IndexFirst, S3);
} break;
case 2:
{
int IndexLast = S1.LastIndexOf(S2);
S1 = S1.Remove(IndexLast, S2.Length).Insert(IndexLast, S3);
} break;
case 3:
{
S1 = S1.Replace(S2, S3);
} break;
}