Name

CharacterIterator

Synopsis

This interface defines an API for portably iterating through the characters that make up a string of text, regardless of the encoding of that text. Such an API is necessary because the number of bytes per character is different for different encodings, and some encodings even use variable-width characters within the same string of text. In addition to allowing iteration, a class that implements the CharacterIterator interface for non-Unicode text also performs translation of characters from their native encoding to standard Java Unicode characters.

CharacterIterator is similar to java.util.Enumeration, but is somewhat more complex than that interface. The first( ) and last( ) methods return the first and last characters in the text, and the next( ) and prev( ) methods allow you to loop forward or backwards through the characters of the text. These methods return the DONE constant when they go beyond the first or last character in the text; a test for this constant can be used to terminate a loop. The CharacterIterator interface also allows random access to the characters in a string of text. The getBeginIndex( ) and getEndIndex( ) methods return the character positions for the start and end of the string, and setIndex( ) sets the current position. getIndex( ) returns the index of the current position, and current( ) returns the character at that position.

Figure 15-3. java.text.CharacterIterator

public interface CharacterIterator extends Cloneable ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.