
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String replaceAll() Method
The Java String replaceAll() method is used to replace all the occurrences of a particular pattern in a String object with the given value.
This method accepts a regular expression and a replacement string as parameters, searches the current string for the given regex and replaces the matched substrings with given replacement string.
The only difference between the replaceAll() method and the replace() method is that the replaceAll() method replaces a pattern (using regex). Whereas the replace() method replaces a String.
Note: Be aware that if the replacement string contains dollar signs ($) or backslashes (\), the results might not be the same as they would be if they were handled as literal replacement strings.
Syntax
Following is the syntax for Java String replaceAll() method −
public String replaceAll(String regex, String replacement)
Parameters
regex − This is the regular expression to which this string is to be matched.
replacement − This is the string to be substituted for each match.
Return Value
This method returns the resulting string.
Replacing a Meta Character from a String Example
The following example shows the usage of Java String replaceAll() method by replacing the meta characters as given in the regular expression −
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str1 = "!!Tutorials!!Point", str2; String substr = "**", regex = "!!"; // prints string1 System.out.println("String = " + str1); /* replaces each substring of this string that matches the given regular expression with the given replacement */ str2 = str1.replaceAll(regex, substr); System.out.println("After Replacing = " + str2); } }
Output
If you compile and run the above program, it will produce the following result −
String = !!Tutorials!!Point After Replacing = **Tutorials**Point
Replacing Occurrences of a Word from a String Example
In the following example all the occurrences of a word is replaced using replaceAll() method −
package com.tutorialspoint; public class StringDemo { public static void main(String args[]) { String str = "Next time there won't be a next time after a certain time."; System.out.println("The given string is: " + str); // replacing all occurrences of "time" to "day" String newString = str.replaceAll("time", "day"); System.out.println("The new replaced string is: " + newString); } }
Output
If you compile and run the program above, the output will be displayed as follows −
The given string is: Next time there won't be a next time after a certain time. The new replaced string is: Next day there won't be a next day after a certain day.
Replacing All Whitespace Character from a String Example
Now, we will remove all the occurrences of white spaces in the given string using replaceAll() method −
package com.tutorialspoint; public class StringDemo { public static void main(String args[]) { String str = "Next time there won't be a next time after a certain time."; System.out.println("The given string is: " + str); // replacing all occurrences of "time" to "day" String newString = str.replaceAll("\\s", ""); System.out.println("The new replaced string is: " + newString); } }
Output
On executing the program above, the output is obtained as follows −
The given string is: Next time there won't be a next time after a certain time. The new replaced string is: Nexttimetherewon'tbeanexttimeafteracertaintime.
Checking PatternSyntaxException for Invalid Pattern Example
Invalid regular expressions cause the replaceAll() method to throw the PatternSyntaxException. Take a look at the example below −
public class StringDemo { public static void main(String argvs[]) { String s = "Welcome to tutorials point."; // invalid regular expression provided. String regExpression = "\\"; s = s.replaceAll(regExpression, "point "); System.out.println("The replaced string is: " + s); } }
PatternSyntaxException
The output of the above program is as follows −
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ at java.base/java.util.regex.Pattern.error(Pattern.java:2038) at java.base/java.util.regex.Pattern.compile(Pattern.java:1799) at java.base/java.util.regex.Pattern.(Pattern.java:1440) at java.base/java.util.regex.Pattern.compile(Pattern.java:1079) at java.base/java.lang.String.replaceAll(String.java:2938) at StringDemo.main(StringDemo.java:7)