Java String replaceAll() Method

Filed Under: String
Java replaceAll() statement

Java String replaceAll() replaces all the occurrences of a particular character, string or a regular expression in the string. The method returns a new string as its output. The method requires two parameters.

String replaceAll() Syntax

public String replaceAll(String regex, String replacement)

A regex that represents the pattern to look for while replacing and a replacement string to replace it with.

There are multiple types of substitutions that are possible with String replaceAll() method.

Replacing a single character

To replace a single character, specify the character to be replaced as the regex.

public class Main {

    public static void main(String[] args) {
        String s = "Welcome to JournalDev";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceAll("o","@");
        System.out.println("New String: "+rep);
    }
}
Replace Single Character 1
Original String: Welcome to JournalDev

New String: Welc@me t@ J@urnalDev

Replace sequence of characters

To replace a sequence of characters, mention the sequence as the regex in the replaceAll() function.

public class Main {

    public static void main(String[] args) {
        String s = "Welcome to JournalDev";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceAll("to","this is");
        System.out.println("New String: "+rep);
    }
}
Replace String
Output
Original String : Welcome to JournalDev

New String: Welcome this is JournalDev

Remove/Replace Spaces

The replaceAll() method can remove or replace the whitespaces in your text string.

public class Main {

    public static void main(String[] args) {
        String s = "Welcome to JournalDev";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceAll(" ","");
        System.out.println("New String: "+rep);
    }
}
Remove Spaces
Remove Spaces
Original String : Welcome to JournalDev

New String: WelcometoJournalDev

Similarly, to replace the whitespaces:

public class Main {

    public static void main(String[] args) {
        String s = "Welcome to JournalDev";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceAll(" ","_");
        System.out.println("New String: "+rep);
    }
}
Replace Space
Original String : Welcome to JournalDev

New String: Welcome_to_JournalDev

Hiding numeric digits in a text

The replaceAll() can hide numeric digits that appear in a string. The digits can be replaced by ‘*’.

public class Main {

    public static void main(String[] args) {
        String s = "Hello! this is the number : 1234 ";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceAll("[0-9]","*");
        System.out.println("New String: "+rep);
    }
}
Hiding Numbers
Original String : Hello! this is the number : 1234 

New String: Hello! this is the number : **** 

Creating custom messages

replaceAll() can replace the strings in a generic string to generate a custom message. Let’s take values from a map and generate a custom message.

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        String s = "dear #Employee#, your Current Salary is #Salary#.";
        Map<String, String> vals = new HashMap<String, String>();
        vals.put("#Employee#", "John");
        vals.put("#Salary#", "12000");
        for (String key : vals.keySet()) {
            s = s.replaceAll(key, vals.get(key));
        }
        System.out.println(s);
    }
}
Custom Messages
dear John, your Current Salary is 12000.

Replace First

Unlike replceAll(), replaceFirst() only replaces the first instance of the pattern.

public class Main {

    public static void main(String[] args) {
        String s = "Welcome to JournalDev";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceFirst("o","@");
        System.out.println("New String: "+rep);
    }
}
ReplaceFirst
Original String: Welcome to JournalDev

New String: Welc@me to JournalDev

Conclusion

The replaceAll() method replaces a regex with a string. This is different from the simple replace() method that only performs character substitutions. You can learn more about the repalceAll() function from its official documentation.

Comments

  1. Deepak says:

    Thank you 馃檪

  2. Vladimir Glinskikh says:

    Very nice example! Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages