Easy Tutorial
❮ String Last Occurance Number Acos ❯

Java replace() Method

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

Return Value

The new string generated after replacement.

Example

The following example replaces characters in the string "tutorialpro":

Example

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

        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:

Return Value :RunTTb
Return Value :RDnoob

Java String Class

❮ String Last Occurance Number Acos ❯