Home Programs Replace a String in String in C#

Replace a String in String in C#

In this example we show you how to replace a string in another string in C#

Example

The Replace() function is used to change the word.

It changes all occurrences of one substring into another substring and print the replaced string.

 

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string myString = "The Biggest River is The Amazon";
            Console.WriteLine("Sentence Before Replacing : {0} ", myString);
            string replacedString = myString.Replace("Amazon", "Nile");
            Console.WriteLine("Sentence After Replacing : {0} ", replacedString);
            Console.ReadLine();
        }
    }
}

Lets see a test run

Sentence Before Replacing : The Biggest River is The Amazon
Sentence After Replacing : The Biggest River is The Nile

You may also like