Java String replaceAll

In this basic tutorial of Java, we will look at the Java String replaceAll method with String class. The Java String replaceAll() method returns a String after it replaces each substring based on the match with a given regular expression. Let’s see this in more details with some examples:

1. String replaceAll() Method

Before we start, let’s take a quick look at the method signature of replaceAll() method:

/**
* Replaces each substring of this string that matches the regular expression with the
* given replacement.
*/
public String replaceAll(String regex, String replacement) {
   return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

Keep in mind that the method will throw PatternSyntaxException if the regular expression’s syntax is invalid.Let’s take an example to understand this:

public class PatternSyntaxExceptionExample {
    public static void main(String[] args) {

        String regex = "["; // invalid regular expression
        String errorStr= "Java Dev Journal example".replaceAll(regex, "error");
    }
}

If we run this code, we will see the following error/ exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
	at java.util.regex.Pattern.error(Pattern.java:1957)
	at java.util.regex.Pattern.clazz(Pattern.java:2550)
	at java.util.regex.Pattern.sequence(Pattern.java:2065)
	at java.util.regex.Pattern.expr(Pattern.java:1998)
	at java.util.regex.Pattern.compile(Pattern.java:1698)
	at java.util.regex.Pattern.(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.replaceAll(String.java:2223)
	at com.javadevjournal.PatternSyntaxExceptionExample.main(PatternSyntaxExceptionExample.java:8)

Let’s take some example for the replaceAll() method

1.1. Replace all matching String occurances

Let’s take an example where we want to replace all occurrences of String with another string. We can accomplish this easily with replaceAll() method.

public class StringReplaceAllExample {

    public static void main(String[] args) {

        String input = "While working on the development environment, all system points to development sandbox";

        String finalOutput= input.replaceAll("development", "production");

        System.out.println(finalOutput);
    }
}

Let’s take another example where we like to replace all the white spaces from a String.We can do that by passing the "\\s" as the regex pattern.

String output = inputStr.replaceAll("\\s", "");

2. String replaceFirst() Method

In case we only want to replace the first substring, we can use the replaceFirst() method

public class StringReplaceFirstExample {

    public static void main(String[] args) {

        String input = "While working on the development environment, all system points to development sandbox";

        String finalOutput= input.replaceFirst("development", "production");

        System.out.println(finalOutput);
    }
}

Output for the above program:

While working on the production environment, all system points to development sandbox

Summary

In this post we saw how to use Java String replaceAll method. We saw how to use this method to replace a String based on the regular expression. At the end of the post, we saw how to use the replaceFirst() method to replace the first String occurrence.