Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5024613

StringIndexOutOfBoundException in String.replaceAll()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2, 5.0
    • core-libs

      Name: rmT116609 Date: 03/31/2004


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

      FULL OS VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      Exception thrown when invoking replaceAll("\\\\", "\\\\\\") on a String.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the testcase below. This will throw the exception.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The documentation is quite vague on the role of backslashes in the replacement string. The sentences:

        "Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string."

      Are confusing. "may cause the result to be different"? depending on what?

      I was trying to replace single occurences of backslashes with double occurrences. At first I thought it should be
        replaceAll("\\\\", "\\\\")

      as the first one is documented as a regexp and the second as a string. This appears to do nothing. Then I tried adding two backslashes to the replacement String, just for fun, and I got the exception.

      ACTUAL -
      an exception

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
      ex out of range: 3
              at java.lang.String.charAt(String.java:444)
              at java.util.regex.Matcher.appendReplacement(Matcher.java:551)
              at java.util.regex.Matcher.replaceAll(Matcher.java:661)
              at java.lang.String.replaceAll(String.java:1663)
              at test.main(test.java:7)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.regex.*;

      public class test {
        static String txt = ".\\try\\this";

        public static void main(String args[]) {
          System.out.println(txt.replaceAll("\\\\", "\\\\\\"));
        }
      }

      ---------- END SOURCE ----------
      (Incident Review ID: 218772)
      ======================================================================

      Name: rmT116609 Date: 03/31/2004


      A DESCRIPTION OF THE PROBLEM :
      replaceAll("\\$", "\\\\$") will raise an exception which is not correct.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      look at this class:
      class TestReplace {
      public static void main(String[] args) {
      String x = "$$$$$";
      String x1 = x.replaceAll("\\$", "\\\\$");
      System.out.println(x1);
      }
      }
      i think the correct output will be "\$\$\$\$\$"

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Actual -
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
              at java.lang.String.charAt(String.java:444)
              at java.util.regex.Matcher.appendReplacement(Matcher.java:559)
              at java.util.regex.Matcher.replaceAll(Matcher.java:661)
              at java.lang.String.replaceAll(String.java:1663)
              at TestReplace.main(TestReplace.java:4)

      Expected -
      failed to replace.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
              at java.lang.String.charAt(String.java:444)
              at java.util.regex.Matcher.appendReplacement(Matcher.java:559)
              at java.util.regex.Matcher.replaceAll(Matcher.java:661)
              at java.lang.String.replaceAll(String.java:1663)
              at TestReplace.main(TestReplace.java:4)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      class TestReplace {
      public static void main(String[] args) {
      String x = "$$$$$";
      String x1 = x.replaceAll("\\$", "\\\\$");
      System.out.println(x1);
      }
      }
      ---------- END SOURCE ----------
      (Review ID: 245724)
      ======================================================================
      ###@###.### 11/1/04 23:18 GMT

            sherman Xueming Shen
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: