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

Java 8Object Oriented ProgrammingProgramming

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!
raja
Published on 06-Dec-2018 16:41:58
Advertisements