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.

Example

The following example shows the usage of Java String replaceAll() method by replacing the meta characters as given in the regular expression −

import java.lang.*; 
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

Example

In the following example all the occurrences of a word is replaced using replaceAll() method −

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.

Example

Now, we will remove all the occurrences of white spaces in the given string using replaceAll() method −

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.

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)
java_lang_string.htm
Advertisements