Java Program to replace all occurrences of a given character with new character



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.

Example

 Live Demo

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'));
   }
}

Output

String = THIS IS DEMO TEXT!
Updated string = THES ES DEMO TEXT!
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements