English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Java other topics

Java String replaceAll() usage and example

Java String (String) Methods

Java String replaceAll() method replaces each substring that matches the regular expression of the string with the specified text.

The syntax of replaceAll() method is:

string.replaceAll(String regex, String replacement)

replaceAll() parameters

The replaceAll() method has two parameters.

  • regex - The regular expression to be replaced (can be a typical string)

  • replacement - The matched substring is replaced by this string

replaceAll() return value

  • The replaceAll() method returns a new string where each occurrence of the matched substring is replaced by the replacement string (replacement).

Example1: Java String replaceAll() method

class Main {
    public static void main(String[] args) {
        String str1 = "aabbaaac";
        String str2 = "Learn223Java55@";
        //Regular expression representing a sequence of digits
        String regex = "\\d+";
        //All occurrences of "aa" are replaced with "zz"
        System.out.println(str1.replaceAll("aa", "zz")); // zzbbzzac
        //Replace digits or sequences of digits with spaces
        System.out.println(str2.replaceAll(regex, " ")); // Learn Java @
    {}
{}

In the above example, "\\d+" is a regular expression that matches one or more digits.

Escaping characters in replaceAll()

The replaceAll() method can take a regular expression or a typical string as the first parameter. This is because a typical string itself is a regular expression.

In regular expressions, some characters have special meanings. These meta characters are:

\ ^ $ . | ? * + {} [] ()

If you need to match substrings containing these meta characters, you can use \ or use the replace() method to escape these characters.

// Program to replace the + character
class Main {
    public static void main(String[] args) {
        String str1 = "+a-+b";
        String str2 = "Learn223Java55@";
        String regex = "\\+";
        // Replace "+"Replace "#" with "#" using the replaceAll() method
        ////Using replaceAll() to replace “ +”Replace with “#”
        System.out.println(str1.replaceAll("\\+", "#")); // #a-#b
        // Replacement+"Replace "#" with "#" using replace() 
        System.out.println(str1.replace("+", "#")); // #a-#b
    {}
{}

As you can see, when we use the replace() method, we do not need to escape the meta-character. For more information, please visit:Java String replace()

If you only need to replace the first match of the matched substring, please useJava String replaceFirst()Methods.

Java String (String) Methods