Search code examples
javastringreplaceall

java find and replace %


In java 1.8.0 I am trying to replace %, but it is not matching

  String str = "%28Sample text%29";

  str.replaceAll("%29", "\\)");

  str.replaceAll("%28", "\\(");
  System.out.println("Replaced string is " + str);

I have tried all this Replace symbol "%" with word "Percent" Nothing worked for me. Thanks in Advance.


Solution

  • It's working. You need re-assign to str

      str = str.replaceAll("%29", "\\)");
    
      str = str.replaceAll("%28", "\\(");