StringCharacterIterator implements the
CharacterIterater protocol for a String.
The StringCharacterIterator class iterates over the
entire String.
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);
}
}
Traverse the text backwards, from end to start
public void traverseBackward(CharacterIterator iter) {
for (char c = iter.last(); c != CharacterIterator.DONE; c = iter.prev()) {
processChar(c);
}
}
Traverse both forward and backward from a given position in the text.
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.prev()) {}
int start = iter.getIndex();
processSection(iter.getText.subString(start,end);
}
Increment the iterator's index by one and return the character
at the new index. If the resulting index is greater or equal
to getEndIndex(), the current index is reset to getEndIndex() and
a value of DONE is returned.
Returns:
the character at the new position or DONE if the current
position is off the end of the text.
Decrement the iterator's index by one and return the character
at the new index. If the resulting index is
less than getBeginIndex(), the current index is reset to getBeginIndex()
and a value of DONE is returned.
Returns:
the character at the new position or DONE if the current
position is off the end of the text.