Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Replace() - Method


Replace() Method


The Replace() Method is used to replace a substring with the new one.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
       	var x = "Hello Beautiful World";
	    var y = x.Replace("Beautiful", "Wonderful");
	 
        System.Console.WriteLine(y);
     }
}


Output :



  Hello Wonderful World

In the above code, we have declared a String Hello Beautiful World and assigned it to a variable x.


var x = "Hello Beautiful World";
C_Sharp


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
C_Sharp