Be careful while using ‘replaceAll’ and ‘replaceFirst’ methods of String class in Java

Most of the time, while trying to replace a substring in a given string, either all its occurrences or just the first one, java programmers tend to use ‘replaceAll’ and ‘replaceFirst’ methods provided by String class in Java. Java programmers like to use these methods to replace substrings as compared to ‘replace’ method of String class because of different reasons like:

  1. ‘replaceAll’ and ‘replaceFirst’ methods use regular expressions instead of character sequence. It is pre-assumed most of the time that using these methods will allow us to replace a given pattern in future, instead of just replacing a substring.
  2. ‘replaceAll’ and ‘replaceFirst’ methods are named such that they clarify the intent of their execution.

However, while using these methods, we need to be very careful while providing the string value which would replace a given substring or a pattern. Let’s understand why with the following example:

Run the following java code:

1
2
3
4
5
6
7
8
9
10
public class StringReplace {

  public static void main(String[] args){
    String stringValue = "test1 test2 test1 test3";
    String replaceValue = "test";
    stringValue = stringValue.replaceAll("test1", replaceValue);
    System.out.print(stringValue);
  }

}

This code works properly. But now try running the same code by changing the value of variable ‘replaceValue’ as:

String replaceValue = "$100";

This would result in an exception of type ‘IndexOutOfBoundException’. This is because the ‘$’ symbol is used to identify a group from the regular expression which is the first parameter of ‘replaceAll’ or ‘replaceFirst’ method. We can solve this problem by:

  1. Using ‘replace’ method: This would be the good choice if you want to replace a string literal and not a pattern.
  2. Escaping ‘$’ symbol: If you need to a use regular expression, and your pattern has no groups identified, then you can escape any group identification symbols from your replace string as shown below:

String replaceValue = java.util.regex.Matcher.quoteReplacement("$100");