Easy Tutorial
❮ Java Hashmap Clear Java Hashmap Putifabsent ❯

Java replaceAll() Method

Java String Class


The replaceAll() method replaces all substrings of the string that match the given regular expression with the specified replacement parameter.

Syntax

public String replaceAll(String regex, String replacement)

Parameters

Return Value

Returns the replaced string if successful, or the original string if unsuccessful.

Example

public class Test {
    public static void main(String args[]) {
        String Str = new String("www.google.com");
        System.out.print("Return value if match is successful: ");
        System.out.println(Str.replaceAll("(.*)google(.*)", "tutorialpro"));
        System.out.print("Return value if match is unsuccessful: ");
        System.out.println(Str.replaceAll("(.*)taobao(.*)", "tutorialpro"));
    }
}

The output of the above program is:

Return value if match is successful: tutorialpro
Return value if match is unsuccessful: www.google.com

Java String Class

❮ Java Hashmap Clear Java Hashmap Putifabsent ❯