All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.StringCharacterIterator

java.lang.Object
   |
   +----java.text.StringCharacterIterator

public final class StringCharacterIterator
extends Object
implements CharacterIterator
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);
 }
 


Constructor Index

 o StringCharacterIterator(String)
Construct an iterator with current index at 0.
 o StringCharacterIterator(String, int)
Construct an iterator with current index at the specified position.

Method Index

 o clone()
Create a copy of this boundary
 o current()
Get the character at the current position (as returned by getIndex()).
 o endIndex()
Return the end index of the text.
 o equals(Object)
Compares the equality of two StringCharacterIterator objects.
 o first()
Set the position to startIndex() and return the character at that position.
 o getIndex()
Return the current index.
 o getText()
Return the text
 o hashCode()
Compute a hashcode for this enumeration
 o last()
Set the position to endIndex() and return the character at that position.
 o next()
Increment the iterator's index by one and return the character at the new index.
 o previous()
Decrement the iterator's index by one and return the character at the new index.
 o setIndex(int)
Set the position to specified position in the text and return that character
 o startIndex()
Return the start index of the text.

Constructors

 o StringCharacterIterator
  public StringCharacterIterator(String text)
Construct an iterator with current index at 0.

Parameters:
text - the text to iterate over.
 o StringCharacterIterator
  public StringCharacterIterator(String text,
                                 int pos)
Construct an iterator with current index at the specified position.

Parameters:
text - the text to iterate over
pos - the initial position of the iterator. If the value of pos is negative or greater than the length of the text, an IllegalArgumentException is thrown.

Methods

 o first
  public char first()
Set the position to startIndex() and return the character at that position.

Returns:
the first character in the text
 o last
  public char last()
Set the position to endIndex() and return the character at that position.

Returns:
the last character in the text
 o setIndex
  public char setIndex(int pos)
Set the position to specified position in the text and return that character

Parameters:
pos - the new scan position. If the value of pos is negative or greater than the length of the text, an IllegalArgumentException is thrown.
Returns:
the character at the specified position.
 o current
  public char current()
Get 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.
 o next
  public char next()
Increment the iterator's index by one and return the character at the new index. If the resulting index is greater or equal to endIndex(), the current index is reset to endIndex() 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.
 o previous
  public char previous()
Decrement the iterator's index by one and return the character at the new index. If the resulting index is less than startIndex(), the current index is reset to startIndex() 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.
 o startIndex
  public int startIndex()
Return the start index of the text.

Returns:
the index at which the text begins.
 o endIndex
  public int endIndex()
Return the end index of the text. This index is the index of the first character following the end of the text.

Returns:
the index at which the text end.
 o getIndex
  public int getIndex()
Return the current index.

Returns:
the current index.
 o getText
  public String getText()
Return the text

 o equals
  public boolean equals(Object obj)
Compares the equality of two StringCharacterIterator objects.

Parameters:
obj - the StringCharacterIterator object to be compared with.
Returns:
true if the given obj is the same as this StringCharacterIterator object; false otherwise.
Overrides:
equals in class Object
 o hashCode
  public int hashCode()
Compute a hashcode for this enumeration

Returns:
A hash code
Overrides:
hashCode in class Object
 o clone
  public Object clone()
Create a copy of this boundary

Returns:
A copy of this
Overrides:
clone in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index