United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-5003547 : (str) add support for iterating over the codepoints in a string

Details
Type:
Enhancement
Submit Date:
2004-02-26
Status:
Closed
Updated Date:
2013-04-22
Project Name:
JDK
Resolved Date:
2013-04-22
Component:
core-libs
OS:
generic
Sub-Component:
java.lang
CPU:
generic
Priority:
P4
Resolution:
Duplicate
Affected Versions:
5.0
Fixed Versions:

Related Reports
Duplicate:
Duplicate:
Relates:

Sub Tasks

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.
                                     
2013-04-22
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.
                                     
2013-04-13
EVALUATION

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



Hardware and Software, Engineered to Work Together