Replace String with Another in Java



The replace() method of the 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
Updated on: 2019-07-30T22:30:21+05:30

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements