Java string replaceAll()

replaceAll() method in java.lang.String class is used to replace every occurrence of a string with some other value in a given string.
String to be replaced may be a simple string or a regular expression and replaceAll() will replace all sequence of characters that match this regular expression.

Java docs of replaceAll() state,

Replaces each substring of this string that matches the given regular expression with the given replacement.

In this article, we will take a look at replaceAll() method with ample examples.

replaceAll() syntax
replaceAll() accepts two arguments:
1. String that needs to be replaced. This may also be a regular expression that is matched in the source string.
2. String that will be replaced.

Signature of replaceAll() is

public String replaceAll(String regex, String replacement)

replaceAll() will return a new string with the desired value replaced. Original string remains unmodified.
This is because string objects are immutable.
replaceAll() example
Below is an example of replaceAll() method to replace all occurrences of a string.

String str = "A website is a collection of web pages";
String replaced = str.replaceAll("web", "blog");
System.out.println(replaced);

This prints

A blogsite is a collection of blog pages

If the string to be replaced is not present in the source string, then replaceAll() does not throw any error and simply returns a copy of original string.
replaceAll() to replace character
replaceAll() can also be used to replace all occurrences of a single character. You simply need to write that character to be replaced as first argument and replacement as the second character. Example,

String str = "flight is full";
str = str.replaceAll("f", "p");
System.out.println(str);

It prints

plight is pull

Remember that replaceAll() is case sensitive. If the string contains “F”, then it will not be replaced with “p”.

replaceAll() regex example
As stated earlier, replaceAll() can be used to replace a string that matches a regular expression.
Below example replaces a string in place of number.

String str = "I love you 2";
str = str.replaceAll("\\d", "too");
System.out.println(str);

\\d is a regular expression which is used to match numbers.
replaceAll("\\d", "too") replaces all the numbers in the string with “too” and the output is

I love you too

For matching multiple digits, use \\d+, where + means one or more.
If the regular expression supplied to replaceAll() is invalid, then an exception is thrown. Thus, below line

str = str.replaceAll("\\", "too");

will throw

Exception in thread “main” java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
at java.base/java.util.regex.Pattern.error(Pattern.java:2028)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1789)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1430)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1069)
at java.base/java.lang.String.replaceAll(String.java:2142)

since \\ is an invalid regular expression.
replaceAll() to replace white spaces
Regular expression \\s is used to match white spaces in a string. replaceAll() can used to remove white spaces from a string as shown below

String str = "java is programming language";
str = str.replaceAll("\\s", "");
System.out.println(str);

Output is

javaisprogramminglanguage

Escaping characters
A regular expression or regex is created using some special characters listed below

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

What if you want to replace one of these characters in a string. You can’t simply write these characters as a first argument.
This is because replaceAll() will not consider them as a string that needs to be replaced. Thus,

String s = "$ is a currency";
s.replaceAll("$", "Dollar");
System.out.println(s);

will print

$ is a currency

That is, it will do nothing.

For replacing any of the regular expression characters with replaceAll(), it needs to be escaped using \\ so that it is considered as a normal string and not as a special character.

String s = "$ is a currency"; 
s.replaceAll("\\$", "Dollar");
System.out.println(s);

This will print

Dollar is a currency

That is all on replaceAll() method in java string to replace a string or regular expression with another string.

0
Liked the article ? Spread the word...