Advertisement



< Prev
Next >



C# Replace() method





As per the name of the method goes, the Replace() method of String class performs either of the two operations:




Two overloads of Replace() method



Methods Description
Replace(char, char)
This method replaces all the occurrences of a character in the invoked String with a specified character value.

Replace(String, String)
This method replaces all the occurrences of a String value in the invoked String with a specified String value.





Note :


If the character to be replaced in a String is not found then String returned by the Replace() method is same as the String that invoked the Replace() method.




Example of the Replace(char, char) method.


Let us show you an example of the first overload version of the Replace() method i.e. Replace(char, char), which replaces all the occurrences of a character in the invoked String with a specified character value.

// C# Program to replace a particular character with different character in a String.

using System;

class StringReplace
{
public static void Main()
{
	//Creating a String object
	String str1= "It is a beautiful day today!";

	Console.WriteLine("Original String is: " + str1);

	//Calling the Replace() method 
	Console.WriteLine("String's value after replacing a with A : " + str1.Replace('a', 'A'));
	
	//Calling the Replace() method 
	Console.WriteLine("String's value after replacing u with U : " + str1.Replace('u', 'U'));

	//Calling the Replace() method 
	Console.WriteLine("String's value after replacing a with x : " + str1.Replace('a', 'x'));

	//Calling the Replace() method
	Console.WriteLine("String's value after replacing t with h : " + str1.Replace('t', 'h'));

	//Calling the Replace() method
	Console.WriteLine("String's value after replacing X with x : " + str1.Replace('X', 'x'));
}

} 


Output -


Original String is: It is a beautiful day today!
String's value after replacing a with A : It is A beAutiful dAy todAy!
String's value after replacing u with U : It is a beaUtifUl day today!
String's value after replacing a with x : It is x bexutiful dxy todxy!
String's value after replacing t with h : Ih is a beauhiful day hoday!
String's value after replacing X with x : It is a beautiful day today!






Method Replace() does not modify the content of invoked String object


Strings are immutable and for those who haven't really looked at the first example of Replace() method closely, the Replace() method cannot modify the original contents of the invoked String.

// C# Replace() method does not modify the content of the invoked String

using System;

class StringReplace2
{
public static void Main()
{
	//Creating a String
	String str1= "Never give up!";

	//Printing the String
	Console.WriteLine("Original String is : "+str1);

	//Calling the Replace() method
	Console.WriteLine("String's value after replacing e with E  : " + str1.Replace('e', 'E'));

	//Printing the String's value after calling Replace() method on it.
	Console.WriteLine("String's value after Replace() is called : " + str1);
}
} 


Output -


Original String is : Never give up!
String's value after replacing a with x : NEvEr givE up!
String value after Replace() is called  : Never give up!


Program Analysis


We have initialized a String object with value Never give up! Post calling the Replace() method, the String object still has the same value Never give up! because String objects are immutable and that's why the Replace() method doesn't modify the invoked String object.


Advertisement




Example of the Replace(String, String) method.


Let us show you an example of the first overload version of the Replace() method i.e. Replace(String, String), which replaces all the occurrences of a specified String value in the invoked String with a specified String value.

// C# Example of the Replace(String, String) method

using System;

class StringReplace2
{
public static void Main()
{
	//Creating a String
	String str1= "When you are in Rome, do how the Romans do";

	//Printing the String
	Console.WriteLine("Original String is : "+str1);

	//Calling the Replace() method
	Console.WriteLine("String's value after replacing Rome with rome : " + str1.Replace("Rome", "ROME"));

	//Calling the Replace() method 
	Console.WriteLine("String's value after replacing you with You : " + str1.Replace("you", "You"));

	//Calling the Replace() method 
	Console.WriteLine("String's value after replacing Do with DO : " + str1.Replace("Do", "DO"));

	//Calling the Replace() method
	Console.WriteLine("String's value after replacing inn with in : " + str1.Replace("inn", "in"));
}
} 


Output -


Original String is : When you are in Rome, do how the Romans do
String's value after replacing Rome with rome : When you are in ROME, do how the Romans do
String's value after replacing you with You : When You are in Rome, do how the Romans do
String's value after replacing Do with DO : When you are in Rome, do how the Romans do
String's value after replacing inn with in : When you are in Rome, do how the Romans do





Please share this article -





< Prev
Next >
< C# EndsWith() Method
C# Insert() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement