The Replace() Method is used to replace a substring with the new one.
public class MyApplication
{
public static void Main(string[] args)
{
var x = "Hello Beautiful World";
var y = x.Replace("Beautiful", "Wonderful");
System.Console.WriteLine(y);
}
}
In the above code, we have declared a String Hello Beautiful World and assigned it to a variable x.
var x = "Hello Beautiful World";
-Method1.png)
And we would be replacing Beautiful with Wonderful, from the String, Hello Beautiful World.
So, we have used the Replace() Method to replace Beautiful with Wonderful.
var y = x.Replace("Beautiful", "Wonderful");As we can see, there are 2 parameters in the Replace("Beautiful", "Wonderful") function.
And thus the substring Beautiful is searched in the String, Hello Beautiful World. When found, Beautiful is replaced with Wonderful. And 1 means exactly 1 occurrence of the substring Beautiful is replaced with Wonderful(We will understand it later in this tutorial).
And the new String becomes,
Hello Wonderful World
-Method2.png)