Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6242664
Votes 0
Synopsis String.offsetByCodePoints doesn't work for Strings returned by String.substring
Category java:classes_lang
Reported Against
Release Fixed mustang(b52), 5.0u19(b01) (Bug ID:2147738)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs
Submit Date 18-MAR-2005
Description
FULL PRODUCT VERSION :
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_01-b08, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux hylas 2.6.9-gentoo-r14 #1 Mon Jan 31 13:57:09 EST 2005 x86_64 AMD Athlon(tm) 64 Processor 3200+ AuthenticAMD GNU/Linux


EXTRA RELEVANT SYSTEM CONFIGURATION :
Tested in Netbeans 4.0

A DESCRIPTION OF THE PROBLEM :
If you get a String back from String.substring(), and try to run .offsetByCodePoints(0,1) on it, it will return a code point index that appears to be relative to the source string (the one you called .subtring() on.)

This is incorrect, since the specification of String.substring() says that it returns a new String().


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the sample code included.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Since I'm running with a basic US locale, I expect the sample code to print

i=1  j=1  k=1

Certainly, there should be no difference between the values printed for j and k.

ACTUAL -
The sample code prints:

i=1  j=4  k=1



ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error codes or exceptions are generated.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package foo;

public class BugTestClass {
    
    public static void main(String args[]) {

        String myString = "abcdef";
        int i = myString.offsetByCodePoints(0,1);
        String sub = myString.substring(3);
        int j = sub.offsetByCodePoints(0,1);
        int k = new String(sub).offsetByCodePoints(0,1);
        System.out.println("i=" + i + "  j=" + j + "  k=" + k);
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
The workaround is to create a new String as in the example for k above, or like so:

String source = "abcdef";
String sub = new String( source.substring(3) );

Now sub.offsetByCodePoints will work as expected.
  xxxxx@xxxxx   2005-03-18 09:18:16 GMT
Work Around
Use Character.offsetByCodePoints(string, index, codePointOffset).
  xxxxx@xxxxx   2005-06-03 08:09:51 GMT
Evaluation
Offset value in String class needs to be considered.
Posted Date : 2005-08-18 04:43:43.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang