How to Replace a String in C#/CSharp

To replace a string in C#/CSharp, we make use Replace method.  Example is given below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.IO;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
string hello = "Hello World";
string myString = hello.Replace("World", "FWait");
Console.WriteLine(myString);
}
}
}
using System; using System.IO; namespace Hello_World { class Program { static void Main(string[] args) { string hello = "Hello World"; string myString = hello.Replace("World", "FWait"); Console.WriteLine(myString); } } }
using System;
using System.IO;

namespace Hello_World
{

    class Program
    {
        static void Main(string[] args)
        {
            string hello = "Hello World";

            string myString = hello.Replace("World", "FWait");

            Console.WriteLine(myString);
        }               
    }
}