Java replace() Method

Java String类Java String Class


The replace() method replaces all occurrences of the searchChar character in the string with the newChar character, and returns the new string after replacement.

Syntax

public String replace(char searchChar, char newChar)    

Parameters

  • searchChar-- the original character.

  • newChar-- the new character.

Return Value

The new string generated after replacement.

Example

The following example replaces the characters in the string Example:

Example

public class Main {
    public static void main(String args[]) {
        String Str = new String("Example");

        System.out.print("Return Value :" );
        System.out.println(Str.replace('o', 'T'));

        System.out.print("Return Value :" );
        System.out.println(Str.replace('u', 'D'));
    }
}

The output of the above program is:

返回值 :RunTTb
返回值 :RDnoob

Java String类Java String Class

Other Extensions