CharacterIterator
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
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);
}
Constant Summary
char |
DONE |
Constant that is returned when the iterator has reached either the end
or the beginning of the text. |
Public Method Summary
abstract
Object
|
clone()
Create a copy of this iterator
|
abstract
char
|
current()
Gets the character at the current position (as returned by getIndex()).
|
abstract
char
|
first()
Sets the position to getBeginIndex() and returns the character at that
position.
|
abstract
int
|
|
abstract
int
|
|
abstract
int
|
|
abstract
char
|
last()
Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
and returns the character at that position.
|
abstract
char
|
next()
Increments the iterator's index by one and returns the character
at the new index.
|
abstract
char
|
previous()
Decrements the iterator's index by one and returns the character
at the new index.
|
abstract
char
|
setIndex(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
clone
()
Create a copy of this iterator
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.
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
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.
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
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()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eCharacterIterator\u003c/code\u003e defines a bidirectional iteration protocol for navigating and processing text.\u003c/p\u003e\n"],["\u003cp\u003eIt allows movement across text using \u003ccode\u003enext()\u003c/code\u003e and \u003ccode\u003eprevious()\u003c/code\u003e, returning \u003ccode\u003eDONE\u003c/code\u003e when reaching boundaries.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetBeginIndex()\u003c/code\u003e and \u003ccode\u003egetEndIndex()\u003c/code\u003e define the text range, while \u003ccode\u003egetIndex()\u003c/code\u003e indicates the current position.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eDONE\u003c/code\u003e constant, with a value of \u003ccode\u003e\\uFFFF\u003c/code\u003e, signifies reaching the beginning or end of the iterable text.\u003c/p\u003e\n"],["\u003cp\u003eThis interface is implemented by \u003ccode\u003eStringCharacterIterator\u003c/code\u003e and \u003ccode\u003eAttributedCharacterIterator\u003c/code\u003e for specific use cases.\u003c/p\u003e\n"]]],["The `CharacterIterator` interface allows bidirectional traversal of text within defined boundaries. Key actions include: getting the start (`getBeginIndex`) and end (`getEndIndex`) indices, retrieving the current index (`getIndex`), and accessing characters at these positions (`first`, `last`, `current`). Iteration is facilitated by `next` and `previous` methods, returning `DONE` when boundaries are reached. The `setIndex` method allows direct positioning, and `clone` makes copies. `DONE` signals the end or beginning of iteration.\n"],null,["# CharacterIterator\n\npublic interface **CharacterIterator** implements [Cloneable](../../../reference/java/lang/Cloneable.html) \n\n|---|---|---|\n| Known Indirect Subclasses [AttributedCharacterIterator](../../../reference/java/text/AttributedCharacterIterator.html), [StringCharacterIterator](../../../reference/java/text/StringCharacterIterator.html) |----------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------| | [AttributedCharacterIterator](../../../reference/java/text/AttributedCharacterIterator.html) | An `AttributedCharacterIterator` allows iteration through both text and related attribute information. | | [StringCharacterIterator](../../../reference/java/text/StringCharacterIterator.html) | `StringCharacterIterator` implements the `CharacterIterator` protocol for a `String`. | |||\n\nThis interface defines a protocol for bidirectional iteration over text.\nThe iterator iterates over a bounded sequence of characters. Characters\nare indexed with values beginning with the value returned by getBeginIndex() and\ncontinuing through the value returned by getEndIndex()-1.\n\n\nIterators maintain a current character index, whose valid range is from\ngetBeginIndex() to getEndIndex(); the value getEndIndex() is included to allow\nhandling of zero-length text ranges and for historical reasons.\nThe current index can be retrieved by calling getIndex() and set directly\nby calling setIndex(), first(), and last().\n\n\nThe methods previous() and next() are used for iteration. They return DONE if\nthey would move outside the range from getBeginIndex() to getEndIndex() -1,\nsignaling that the iterator has reached the end of the sequence. DONE is\nalso returned by other methods to indicate that the current index is\noutside this range.\n\nExamples:\n\n\nTraverse the text from start to finish \n\n public void traverseForward(CharacterIterator iter) {\n for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {\n processChar(c);\n }\n }\n \nTraverse the text backwards, from end to start \n\n public void traverseBackward(CharacterIterator iter) {\n for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {\n processChar(c);\n }\n }\n \nTraverse both forward and backward from a given position in the text. Calls to notBoundary() in this example represents some additional stopping criteria. \n\n public void traverseOut(CharacterIterator iter, int pos) {\n for (char c = iter.setIndex(pos);\n c != CharacterIterator.DONE && notBoundary(c);\n c = iter.next()) {\n }\n int end = iter.getIndex();\n for (char c = iter.setIndex(pos);\n c != CharacterIterator.DONE && notBoundary(c);\n c = iter.previous()) {\n }\n int start = iter.getIndex();\n processSection(start, end);\n }\n \n\u003cbr /\u003e\n\n##### See Also\n\n- [StringCharacterIterator](../../../reference/java/text/StringCharacterIterator.html)\n- [AttributedCharacterIterator](../../../reference/java/text/AttributedCharacterIterator.html) \n\n### Constant Summary\n\n|------|------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|\n| char | [DONE](../../../reference/java/text/CharacterIterator.html#DONE) | Constant that is returned when the iterator has reached either the end or the beginning of the text. |\n\n### Public Method Summary\n\n|-------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Object](../../../reference/java/lang/Object.html) | [clone](../../../reference/java/text/CharacterIterator.html#clone())() Create a copy of this iterator |\n| abstract char | [current](../../../reference/java/text/CharacterIterator.html#current())() Gets the character at the current position (as returned by getIndex()). |\n| abstract char | [first](../../../reference/java/text/CharacterIterator.html#first())() Sets the position to getBeginIndex() and returns the character at that position. |\n| abstract int | [getBeginIndex](../../../reference/java/text/CharacterIterator.html#getBeginIndex())() Returns the start index of the text. |\n| abstract int | [getEndIndex](../../../reference/java/text/CharacterIterator.html#getEndIndex())() Returns the end index of the text. |\n| abstract int | [getIndex](../../../reference/java/text/CharacterIterator.html#getIndex())() Returns the current index. |\n| abstract char | [last](../../../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. |\n| abstract char | [next](../../../reference/java/text/CharacterIterator.html#next())() Increments the iterator's index by one and returns the character at the new index. |\n| abstract char | [previous](../../../reference/java/text/CharacterIterator.html#previous())() Decrements the iterator's index by one and returns the character at the new index. |\n| abstract char | [setIndex](../../../reference/java/text/CharacterIterator.html#setIndex(int))(int position) Sets the position to the specified position in the text and returns that character. |\n\nConstants\n---------\n\n#### public static final char\n**DONE**\n\nConstant that is returned when the iterator has reached either the end\nor the beginning of the text. The value is '\\\\\\\\uFFFF', the \"not a\ncharacter\" value which should not occur in any valid Unicode string. \nConstant Value: 65535\n\nPublic Methods\n--------------\n\n#### public abstract [Object](../../../reference/java/lang/Object.html)\n**clone**\n()\n\nCreate a copy of this iterator \n\n##### Returns\n\n- A copy of this \n\n#### public abstract char\n**current**\n()\n\nGets the character at the current position (as returned by getIndex()). \n\n##### Returns\n\n- the character at the current position or DONE if the current position is off the end of the text. \n\n##### See Also\n\n- [getIndex()](../../../reference/java/text/CharacterIterator.html#getIndex()) \n\n#### public abstract char\n**first**\n()\n\nSets the position to getBeginIndex() and returns the character at that\nposition. \n\n##### Returns\n\n- the first character in the text, or DONE if the text is empty \n\n##### See Also\n\n- [getBeginIndex()](../../../reference/java/text/CharacterIterator.html#getBeginIndex()) \n\n#### public abstract int\n**getBeginIndex**\n()\n\nReturns the start index of the text. \n\n##### Returns\n\n- the index at which the text begins. \n\n#### public abstract int\n**getEndIndex**\n()\n\nReturns the end index of the text. This index is the index of the first\ncharacter following the end of the text. \n\n##### Returns\n\n- the index after the last character in the text \n\n#### public abstract int\n**getIndex**\n()\n\nReturns the current index. \n\n##### Returns\n\n- the current index. \n\n#### public abstract char\n**last**\n()\n\nSets the position to getEndIndex()-1 (getEndIndex() if the text is empty)\nand returns the character at that position. \n\n##### Returns\n\n- the last character in the text, or DONE if the text is empty \n\n##### See Also\n\n- [getEndIndex()](../../../reference/java/text/CharacterIterator.html#getEndIndex()) \n\n#### public abstract char\n**next**\n()\n\nIncrements the iterator's index by one and returns the character\nat the new index. If the resulting index is greater or equal\nto getEndIndex(), the current index is reset to getEndIndex() and\na value of DONE is returned. \n\n##### Returns\n\n- the character at the new position or DONE if the new position is off the end of the text range. \n\n#### public abstract char\n**previous**\n()\n\nDecrements the iterator's index by one and returns the character\nat the new index. If the current index is getBeginIndex(), the index\nremains at getBeginIndex() and a value of DONE is returned. \n\n##### Returns\n\n- the character at the new position or DONE if the current position is equal to getBeginIndex(). \n\n#### public abstract char\n**setIndex**\n(int position)\n\nSets the position to the specified position in the text and returns that\ncharacter. \n\n##### Parameters\n\n| position | the position within the text. Valid values range from getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown if an invalid value is supplied. |\n|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- the character at the specified position or DONE if the specified position is equal to getEndIndex()"]]