Advertisement
public String replace(char oldChar, char newChar)
// Program to replace a particular character with different character in a String.
class StringReplace
{
public static void main(String[] ar)
{
String str1= new String("Java Rules");
System.out.println("Original String is : " + str1);
System.out.println("String's value after replacing a with x : " + str1.replace('a', 'x'));
}
}
Original String is : Java Rules
String's value after replacing a with x : Jxvx Rules
// Program to replace a particular character with different character in a String.
class StringReplace2
{
public static void main(String[] ar)
{
String str1= new String("Never give up!");
System.out.println("Original String is : "+str1);
System.out.println("String's value after replacing e with E : " + str1.replace('e', 'E'));
System.out.println("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
// Program to modify the invoked String object with the result of replace() method.
class StringReplace3
{
public static void main(String[] ar)
{
String str1= new String("Java Rules");
System.out.println("Original String is : "+str1);
str1 = str1.replace('a', 'x');
System.out.println("String's value after replacing a with x : " + str1);
}
}
Original String is : Java Rules
String's value after replacing a with x : Jxvx Rules
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement