-
Bug
-
Resolution: Duplicate
-
P3
-
None
-
1.4.2
-
x86
-
windows_xp
Name: gm110360 Date: 12/15/2003
FULL PRODUCT VERSION :
java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
I think all platform
EXTRA RELEVANT SYSTEM CONFIGURATION :
nothing
A DESCRIPTION OF THE PROBLEM :
Error when replace string "\\" by "/", or replace string "/" by "\\". or replace other more longer string contains "/" or "\\".
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
sample code:
String str = "abcd\\efg//hijk:lmn";
System.out.println("str = " + str);
str = str.replaceAll("/", "\\"); //error,
System.out.println("after replace / with \\ =" + str);
str = str.replaceAll("\\", "/"); //error,
System.out.println("after replace \\ with / =" + str);
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
expect string replace ok.
Other if need to replace by other way, the Java doc of Class String should point out this.
ACTUAL -
compile ok.
runtime error.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
1. error message of code str = str.replaceAll("/", "\\");
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:460)
at java.util.regex.Matcher.appendReplacement(Matcher.java:544)
at java.util.regex.Matcher.replaceAll(Matcher.java:646)
at java.lang.String.replaceAll(String.java:1710)
2. error message of code str = str.replaceAll("\\", "/");
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
^
at java.util.regex.Pattern.error(Pattern.java:1489)
at java.util.regex.Pattern.compile(Pattern.java:1281)
at java.util.regex.Pattern.<init>(Pattern.java:1030)
at java.util.regex.Pattern.compile(Pattern.java:777)
at java.lang.String.replaceAll(String.java:1710)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
String str = "abcd\\efg//hijk:lmn";
System.out.println("str = " + str);
str = str.replaceAll("/", "\\"); //error,
System.out.println("after replace / with \\ =" + str);
str = str.replaceAll("\\", "/"); //error,
System.out.println("after replace \\ with / =" + str);
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
maybe should modify function
public Matcher appendReplacement(StringBuffer sb, String replacement)
in java.util.regex.Matcher
(Incident Review ID: 230979)
======================================================================
- duplicates
-
JDK-5024613 StringIndexOutOfBoundException in String.replaceAll()
-
- Closed
-