JDK-5003547 : (str) add support for iterating over the codepoints in a string
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2004-02-26
  • Updated: 2013-04-22
  • Resolved: 2013-04-22
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
[adapted from Josh and Neal's "cooking with Tiger" talk]

Placing the enclosed adapter method in java.lang.String enables clients to
iterate through the code points in a String very easily:

    void f(String s) {
	for (int ch : s.codePoints())
	    ;// do something with the codepoint ch here.
    }

Here is the proposed addition to java.lang.String:

public Iterable<Integer> codePoints() {
    return new Iterable<Integer>() {
        public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                int nextIndex = 0;
                public boolean hasNext() {
                    return nextIndex < length();
                }
                public Integer next() {
                    int result = codePointAt(nextIndex);
                    nextIndex += Character.charCount(result);
                    return result;
                }
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

Comments
Functionality is being added in JDK 8 by JDK-8012665.
22-04-2013

Old blog entry on this topic: https://blogs.oracle.com/darcy/entry/iterating_over_codepoints This enhancement should be considered in the context of stream support and other string updates being added in JDK 8.
13-04-2013

EVALUATION Consider this for the next functionality release. ###@###.### 2004-03-02
02-03-2004