Package java.text
Class StringCharacterIterator
java.lang.Object
java.text.StringCharacterIterator
- All Implemented Interfaces:
Cloneable,CharacterIterator
public final class StringCharacterIterator extends Object implements CharacterIterator
An implementation of
CharacterIterator for strings.-
Field Summary
Fields inherited from interface java.text.CharacterIterator
DONE -
Constructor Summary
Constructors Constructor Description StringCharacterIterator(String value)Constructs a newStringCharacterIteratoron the specified string.StringCharacterIterator(String value, int location)Constructs a newStringCharacterIteratoron the specified string with the current index set to the specified value.StringCharacterIterator(String value, int start, int end, int location)Constructs a newStringCharacterIteratoron the specified string with the begin, end and current index set to the specified values. -
Method Summary
Modifier and Type Method Description Objectclone()Returns a newStringCharacterIteratorwith the same source string, begin, end, and current index as this iterator.charcurrent()Returns the character at the current index in the source string.booleanequals(Object object)Compares the specified object with thisStringCharacterIteratorand indicates if they are equal.charfirst()Sets the current position to the begin index and returns the character at the new position in the source string.intgetBeginIndex()Returns the begin index in the source string.intgetEndIndex()Returns the end index in the source string.intgetIndex()Returns the current index in the source string.inthashCode()Returns an integer hash code for this object.charlast()Sets the current position to the end index - 1 and returns the character at the new position.charnext()Increments the current index and returns the character at the new index.charprevious()Decrements the current index and returns the character at the new index.charsetIndex(int location)Sets the current index in the source string.voidsetText(String value)Sets the source string to iterate over.
-
Constructor Details
-
StringCharacterIterator
Constructs a newStringCharacterIteratoron the specified string. The begin and current indices are set to the beginning of the string, the end index is set to the length of the string.- Parameters:
value- the source string to iterate over.
-
StringCharacterIterator
Constructs a newStringCharacterIteratoron the specified string with the current index set to the specified value. The begin index is set to the beginning of the string, the end index is set to the length of the string.- Parameters:
value- the source string to iterate over.location- the current index.- Throws:
IllegalArgumentException- iflocationis negative or greater than the length of the source string.
-
StringCharacterIterator
Constructs a newStringCharacterIteratoron the specified string with the begin, end and current index set to the specified values.- Parameters:
value- the source string to iterate over.start- the index of the first character to iterate.end- the index one past the last character to iterate.location- the current index.- Throws:
IllegalArgumentException- ifstart < 0,start > end,location < start,location > endor ifendis greater than the length ofvalue.
-
-
Method Details
-
clone
Returns a newStringCharacterIteratorwith the same source string, begin, end, and current index as this iterator.- Specified by:
clonein interfaceCharacterIterator- Overrides:
clonein classObject- Returns:
- a shallow copy of this iterator.
- See Also:
Cloneable
-
current
public char current()Returns the character at the current index in the source string.- Specified by:
currentin interfaceCharacterIterator- Returns:
- the current character, or
DONEif the current index is past the end.
-
equals
Compares the specified object with thisStringCharacterIteratorand indicates if they are equal. In order to be equal,objectmust be an instance ofStringCharacterIteratorthat iterates over the same sequence of characters with the same index.- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this object.- Returns:
trueif the specified object is equal to thisStringCharacterIterator;falseotherwise.- See Also:
hashCode()
-
first
public char first()Sets the current position to the begin index and returns the character at the new position in the source string.- Specified by:
firstin interfaceCharacterIterator- Returns:
- the character at the begin index or
DONEif the begin index is equal to the end index.
-
getBeginIndex
public int getBeginIndex()Returns the begin index in the source string.- Specified by:
getBeginIndexin interfaceCharacterIterator- Returns:
- the index of the first character of the iteration.
-
getEndIndex
public int getEndIndex()Returns the end index in the source string.- Specified by:
getEndIndexin interfaceCharacterIterator- Returns:
- the index one past the last character of the iteration.
-
getIndex
public int getIndex()Returns the current index in the source string.- Specified by:
getIndexin interfaceCharacterIterator- Returns:
- the current index.
-
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
last
public char last()Sets the current position to the end index - 1 and returns the character at the new position.- Specified by:
lastin interfaceCharacterIterator- Returns:
- the character before the end index or
DONEif the begin index is equal to the end index.
-
next
public char next()Increments the current index and returns the character at the new index.- Specified by:
nextin interfaceCharacterIterator- Returns:
- the character at the next index, or
DONEif the next index would be past the end.
-
previous
public char previous()Decrements the current index and returns the character at the new index.- Specified by:
previousin interfaceCharacterIterator- Returns:
- the character at the previous index, or
DONEif the previous index would be past the beginning.
-
setIndex
public char setIndex(int location)Sets the current index in the source string.- Specified by:
setIndexin interfaceCharacterIterator- Parameters:
location- the index the current position is set to.- Returns:
- the character at the new index, or
DONEiflocationis set to the end index. - Throws:
IllegalArgumentException- iflocationis smaller than the begin index or greater than the end index.
-
setText
Sets the source string to iterate over. The begin and end positions are set to the start and end of this string.- Parameters:
value- the new source string.
-