JDK-7067045 : replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 6,6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2011-07-14
  • Updated: 2014-11-20
  • Resolved: 2012-05-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b36Fixed
Related Reports
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
  public void test(){
    String s1 = "\u20ac";
// or
// String s1 = "���";
    s1 = s1.replaceAll("\u20ac", "$");
//  or
//  s1 = s1.replaceAll("���", "$");
    System.out.println(s1);
  }

causes
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run:

  public void test(){
    String s1 = "\u20ac";
// or
// String s1 = "���";
    s1 = s1.replaceAll("\u20ac", "$");
//  or
//  s1 = s1.replaceAll("���", "$");
    System.out.println(s1);
  }

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
output of $ not an exception
ACTUAL -
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
	at java.lang.String.charAt(Unknown Source)
	at java.util.regex.Matcher.appendReplacement(Unknown Source)
	at java.util.regex.Matcher.replaceAll(Unknown Source)
	at java.lang.String.replaceAll(Unknown Source)
...

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

  public void test(){
    String s1 = "\u20ac";
// or
// String s1 = "���";
    s1 = s1.replaceAll("\u20ac", "$");
//  or
//  s1 = s1.replaceAll("���", "$");
    System.out.println(s1);
  }
---------- END SOURCE ----------

Comments
EVALUATION "$" and backslash has special meaning in replacement string. use s1 = s1.replaceAll("\u20ac", "\\$"); instead. It appears, as the existing source comment suggested, IllegalArgumentException probably is a more appropriate exception to throw in this case. Change to RFE
12-04-2012