Let’s say the following is our string.
THIS IS DEMO TEXT!
Here, to replace every occurrence of ‘I’ with ‘E’, use the replace() method.
str.replace('I', 'E'));
The following is the complete example to replace all occurrences of a given character with new character.
public class Demo { public static void main(String[] args) { String str = "THIS IS DEMO TEXT!"; System.out.println("String = "+str); System.out.println("Updated string = "+str.replace('I', 'E')); } }
String = THIS IS DEMO TEXT! Updated string = THES ES DEMO TEXT!