Advertisement
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. |
// 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'));
}
}
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!
// 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);
}
}
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!
Advertisement
// 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"));
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement