java.lang.String replaceAll()

Description :

This java tutorial shows how to use the replaceAll() method of java.lang.String class. This method returns a new String object as a result of replacing all the occurrences of the regex pattern with String parameter replacement.

Method Syntax :

public String replaceAll(String regex,String replacement)

Parameter Input :

DataType Parameter Description
String regex Regular expression that we have to use on evaluation if our string qualifies to be reconstructed by replacing characters.
String replacement The string to be replaced on our String once the regex pattern satisfies our method search

Method Returns :

This method returns a new String object as a result of replacing all the occurrences of regex pattern with the replacement method argument.

Compatibility Version :

Requires Java 1.4 and up

Exception :

PatternSyntaxException

This exception will be thrown if and only if the regex pattern is invalid

Discussion :

The regex string declared as a parameter on method replaceAll() will serve as a searching pattern in replacing all the occurrences of our String replacement.

Be aware that in using the method replaceAll will not make our String change its value. Once a String has been instantiated, it’s no longer changeable. Thus, if we want to use the new String object what we can do is to reassign it again to our String object or create a new String object.

Let’s take for example:

String exampleString = "1234123";
exampleString.replaceAll("1","x");
System.out.println(exampleString);

What would you expect to be printed on the console? Would it be “x234x23” or “1234124”? As we have said earlier, invoking the replaceAll method will not change the String object exampleString, rather it will create a new String object.

Java Code Example :

This example source code demonstrates the use of replaceAll method of String class. This is a simple replacement of all the occurrences of String “0” to “**”.

package com.javatutorialhq.java.tutorial.string;

/*
 * Example source code to replace all string occurrences
 */

public class StringReplaceAll {

    public static void main(String[] args) {
        // Declare the source string
        String stringValue = "102345000";
        // Define the regex pattern
        String regex = "0";
        // Replacing all the occurrences of String "0"
        String objString = stringValue.replaceAll("0", "**");
        System.out.println("New String Object: "+objString);
    }

}

Sample Output :

Running the replaceAll() method example source code of [class name here] class will give you the following output

java string replaceall method example

java string replaceall method example

Exception Scenario :

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*
^
	at java.util.regex.Pattern.error(Unknown Source)
	at java.util.regex.Pattern.sequence(Unknown Source)
	at java.util.regex.Pattern.expr(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.util.regex.Pattern.(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.lang.String.replaceAll(Unknown Source)
	at com.teknoscope.java.tutorial.string.StringReplaceAll.main(StringReplaceAll.java:16)

Suggested Reading List :