# CharacterIterator

public interface **CharacterIterator** implements [Cloneable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Cloneable.html)

|---|---|---|
| Known Indirect Subclasses [AttributedCharacterIterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/AttributedCharacterIterator.html), [StringCharacterIterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/StringCharacterIterator.html) |---|---| | [AttributedCharacterIterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/AttributedCharacterIterator.html) | An `AttributedCharacterIterator` allows iteration through both text and related attribute information. | | [StringCharacterIterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/StringCharacterIterator.html) | `StringCharacterIterator` implements the `CharacterIterator` protocol for a `String`. | |||

This interface defines a protocol for bidirectional iteration over text.
The iterator iterates over a bounded sequence of characters. Characters
are indexed with values beginning with the value returned by getBeginIndex() and
continuing through the value returned by getEndIndex()-1.


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);
         }
     }
     
Traverse the text backwards, from end to start

    public void traverseBackward(CharacterIterator iter) {
         for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
             processChar(c);
         }
     }
     
Traverse both forward and backward from a given position in the text. Calls to notBoundary() in this example represents some additional stopping criteria.

    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);
     }
     
<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/text/StringCharacterIterator.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/text/AttributedCharacterIterator.html`

### Constant Summary

|---|---|---|
| char | [DONE](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#DONE) | Constant that is returned when the iterator has reached either the end or the beginning of the text. |

### Public Method Summary

|---|---|
| abstract [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#clone())() Create a copy of this iterator |
| abstract char | [current](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#current())() Gets the character at the current position (as returned by getIndex()). |
| abstract char | [first](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#first())() Sets the position to getBeginIndex() and returns the character at that position. |
| abstract int | [getBeginIndex](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#getBeginIndex())() Returns the start index of the text. |
| abstract int | [getEndIndex](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#getEndIndex())() Returns the end index of the text. |
| abstract int | [getIndex](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#getIndex())() Returns the current index. |
| abstract char | [last](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#last())() Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position. |
| abstract char | [next](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#next())() Increments the iterator's index by one and returns the character at the new index. |
| abstract char | [previous](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#previous())() Decrements the iterator's index by one and returns the character at the new index. |
| abstract char | [setIndex](https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#setIndex(int))(int position) Sets the position to the specified position in the text and returns that character. |

## Constants

#### public static final char
**DONE**

Constant that is returned when the iterator has reached either the end
or the beginning of the text. The value is '\\\\uFFFF', the "not a
character" value which should not occur in any valid Unicode string.
Constant Value: 65535

## Public Methods

#### public abstract [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**clone**
()

Create a copy of this iterator

##### Returns

- A copy of this

#### public abstract char
**current**
()

Gets the character at the current position (as returned by getIndex()).

##### Returns

- the character at the current position or DONE if the current position is off the end of the text.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#getIndex()`

#### public abstract char
**first**
()

Sets the position to getBeginIndex() and returns the character at that
position.

##### Returns

- the first character in the text, or DONE if the text is empty

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#getBeginIndex()`

#### public abstract int
**getBeginIndex**
()

Returns the start index of the text.

##### Returns

- the index at which the text begins.

#### public abstract int
**getEndIndex**
()

Returns the end index of the text. This index is the index of the first
character following the end of the text.

##### Returns

- the index after the last character in the text

#### public abstract int
**getIndex**
()

Returns the current index.

##### Returns

- the current index.

#### public abstract char
**last**
()

Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
and returns the character at that position.

##### Returns

- the last character in the text, or DONE if the text is empty

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/text/CharacterIterator.html#getEndIndex()`

#### public abstract char
**next**
()

Increments the iterator's index by one and returns 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 new position is off the end of the text range.

#### public abstract char
**previous**
()

Decrements the iterator's index by one and returns the character
at the new index. If the current index is getBeginIndex(), the index
remains at getBeginIndex() and a value of DONE is returned.

##### Returns

- the character at the new position or DONE if the current position is equal to getBeginIndex().

#### public abstract char
**setIndex**
(int position)

Sets the position to the specified position in the text and returns that
character.

##### Parameters

| position | the position within the text. Valid values range from getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown if an invalid value is supplied. |
|---|---|

##### Returns

- the character at the specified position or DONE if the specified position is equal to getEndIndex()