JDK-5063163 : (str) Please add a method int[] String.toCodePointArray()
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2004-06-15
  • Updated: 2017-07-20
  • Resolved: 2017-07-20
Related Reports
Relates :  
Description
Name: js151677			Date: 06/15/2004


A DESCRIPTION OF THE REQUEST :
With Unicode 4 and supplementary characters, it has become painful to process Strings as char sequences. It would be easier to handle them as int sequences. The low-hanging fruit would be to supply a method

int[] toCodePointArray()

in the String class (and, if you feel generous, a constructor String(int[]) in addition to the existing String(int[], int, int))



JUSTIFICATION :
Consider a typical string processing task--removing characters that match a particular criterion. It is painful to handle the surrogate characters.

In addition, it would be delightful to be able to iterate over the code points with the generalized for loop:

for (int cp : str.toCodePointArray()) { ... }



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Right now, I use a helper method such as this one:

 private static int[] getCodePointArray(String str)
   {
      int[] codePoints = new int[str.codePointCount(0, str.length())];
      for (int i = 0, j = 0; i < str.length(); i++, j++)
      {
         int cp = str.codePointAt(i);
         if (Character.isSupplementaryCodePoint(cp)) i++;
         codePoints[j] = cp;
      }
      return codePoints;
   }
ACTUAL -
Ugh, don't get me going
(Incident Review ID: 276989) 
======================================================================

Comments
This can easily be done with streams in Java 8: int[] cp = str.codePoints().toArray(); Closing as Not an Issue.
20-07-2017

EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=18210
18-01-2007