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

String.ReplaceAll() gives java.lang.StringIndexOutOfBoundsException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • 1.4.1, 1.4.2
    • core-libs
    • None
    • x86, sparc
    • solaris_8, windows_2000

      Following is the test program
      ----------------------------
      public class slash {
              public static void main (String str[]){
                      String s = "HixyzHelloxyz";
                      System.out.println(s);
                      //s = s.replaceAll("xyz","$");
                      s = s.replaceAll("xyz","\\");
                      System.out.println(s);
              }
      }

      $ /net/sqindia/export/disk01/jdk/1.4.1/beta/b12/binaries/solsparc/bin/java -version
      java version "1.4.1-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b12)
      Java HotSpot(TM) Client VM (build 1.4.1-beta-b12, mixed mode)

      Output:
      ---------
      HixyzHelloxyz
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
              at java.lang.String.charAt(String.java:455)
              at java.util.regex.Matcher.appendReplacement(Matcher.java:532)
              at java.util.regex.Matcher.replaceAll(Matcher.java:636)
              at java.lang.String.replaceAll(String.java:1706)
              at slash.main(slash.java:6)

      Spec says 'An invocation of this (replaceAll) method of the form str. replaceAll (regex, repl) yields exactly the same result as the expression
      Pattern.compile(regex).matcher(str).replaceAll(repl)

      However, this is also results into same error.
      For Example:
      import java.util.regex.*;
      public class t{
              public static void main (String str[]){
                      String s = "HixyzHelloxyz";
                      Pattern.compile("xyz").matcher(s).replaceAll("$");
                      System.out.println(s);
              }
      }

      This also exists in Merlin- FCS (b92).

            mmcclosksunw Michael Mccloskey (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: