Cloneable
AttributedCharacterIterator
Segment
, StringCharacterIterator
public interface CharacterIterator extends Cloneable
Iterators maintain a current character index, whose valid range is from getBeginIndex() to getEndIndex(); the value getEndIndex() is included to allow handling of zero-length text ranges and for historical reasons. The current index can be retrieved by calling getIndex() and set directly by calling setIndex(), first(), and last().
The methods previous() and next() are used for iteration. They return DONE if they would move outside the range from getBeginIndex() to getEndIndex() -1, signaling that the iterator has reached the end of the sequence. DONE is also returned by other methods to indicate that the current index is outside this range.
Examples:
Traverse the text from start to finish
public void traverseForward(CharacterIterator iter) {
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
processChar(c);
}
}
public void traverseBackward(CharacterIterator iter) {
for (char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
processChar(c);
}
}
public void traverseOut(CharacterIterator iter, int pos) {
for (char c = iter.setIndex(pos);
c != CharacterIterator.DONE && notBoundary(c);
c = iter.next()) {
}
int end = iter.getIndex();
for (char c = iter.setIndex(pos);
c != CharacterIterator.DONE && notBoundary(c);
c = iter.previous()) {
}
int start = iter.getIndex();
processSection(start, end);
}
Modifier and Type | Field | Description |
---|---|---|
static final char |
DONE |
Constant that is returned when the iterator has reached either the end or the beginning of the text. |
Modifier and Type | Method | Description |
---|---|---|
Object |
clone() |
Create a copy of this iterator |
char |
current() |
Gets the character at the current position (as returned by getIndex()). |
char |
first() |
Sets the position to getBeginIndex() and returns the character at that position. |
int |
getBeginIndex() |
Returns the start index of the text. |
int |
getEndIndex() |
Returns the end index of the text. |
int |
getIndex() |
Returns the current index. |
char |
last() |
Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position. |
char |
next() |
Increments the iterator's index by one and returns the character at the new index. |
char |
previous() |
Decrements the iterator's index by one and returns the character at the new index. |
char |
setIndex |
Sets the position to the specified position in the text and returns that character. |
static final char DONE
char first()
char last()
char current()
char next()
char previous()
char setIndex(int position)
position
- the position within the text. Valid values range from getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown if an invalid value is supplied.int getBeginIndex()
int getEndIndex()
int getIndex()
Object clone()
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/text/CharacterIterator.html