Java String replace() method example.

Java 8Object Oriented ProgrammingProgramming

The replace() method of the String class accepts two parameters namely −

  • oldChar − the old character.
  •  newChar − the new character.

returns a new String resulting from replacing all occurrences of oldChar in this string with newChar.

Example

Live Demo

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");
      System.out.print("Return Value :" );
      System.out.println(Str.replace('o', 'T'));
      System.out.print("Return Value :" );
      System.out.println(Str.replace('l', 'D'));
   }
}

Output

Return Value :WelcTme tT TutTrialspTint.cTm
Return Value :WeDcome to TutoriaDspoint.com
raja
Published on 12-Feb-2018 11:03:10
Advertisements