Class CharacterIterator


  • public class CharacterIterator
    extends java.lang.Object
    A class for bidirectional iteration over a string. Iterators maintain a current character index, whose valid range is from 0 to string.length()-1. The current index can be retrieved by calling getIndex() and set directly by calling setIndex(). The methods previous() and next() are used for iteration. They return DONE if they would move outside the range from 0 to string.length()-1.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char DONE
      Constant that is returned when the iterator has reached either the end or the beginning of the text.
    • Constructor Summary

      Constructors 
      Constructor Description
      CharacterIterator​(java.lang.String str)
      Constructs a new character iterator using str.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int find​(char c)
      Looks for the next occurrence of 'c' starting at the current index.
      char getAndIncrement()
      Returns the character at the current index and then increments the index by one.
      int getIndex()
      Returns the current index.
      int getLength()
      Returns the length of the iterator.
      java.lang.String getString()
      Returns the underlying string.
      boolean hasNext()
      Returns true if there are more characters to read.
      char next()
      Increments the current index by one and returns the character at the new index.
      int nextInteger()
      Returns the next integer.
      java.lang.String nextString​(int len)
      Returns the next ascii string of the specified length starting at the current index.
      char peek()
      Returns the next character without incrementing the current index.
      char peek​(int lookAhead)
      Peeks at the character current index + lookAhead.
      char previous()
      Decrements the current index by one and returns the character at the new index.
      void setIndex​(int index)
      Sets the position to the specified position in the text.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DONE

        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.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CharacterIterator

        public CharacterIterator​(java.lang.String str)
        Constructs a new character iterator using str.
        Parameters:
        str - the string to iterate
    • Method Detail

      • getString

        public java.lang.String getString()
        Returns the underlying string.
        Returns:
        the underlying string
      • getIndex

        public int getIndex()
        Returns the current index.
        Returns:
        the current index.
      • getLength

        public int getLength()
        Returns the length of the iterator.
        Returns:
        the length of the iterator
      • setIndex

        public void setIndex​(int index)
        Sets the position to the specified position in the text.
        Parameters:
        index - the position within the text.
      • hasNext

        public boolean hasNext()
        Returns true if there are more characters to read.
        Returns:
        true if there are more characters to read
      • peek

        public char peek()
        Returns the next character without incrementing the current index.
        Returns:
        the next character without incrementing the current index
      • peek

        public char peek​(int lookAhead)
        Peeks at the character current index + lookAhead. Returns DONE if the computed position is out of range.
        Parameters:
        lookAhead - number of characters to look ahead
        Returns:
        the character at index+lookAhead
      • next

        public char next()
        Increments the current index by one and returns the character at the new index. If the resulting index is greater or equal to the end index, the current index is reset to the end index and a value of DONE is returned.
        Returns:
        the character at the new position or DONE
      • getAndIncrement

        public char getAndIncrement()
        Returns the character at the current index and then increments the index by one. If the resulting index is greater or equal to the end index, the current index is reset to the end index and a value of DONE is returned.
        Returns:
        the character at the new position or DONE
      • previous

        public char previous()
        Decrements the current index by one and returns the character at the new index. If the current index is 0, the index remains at 0 and a value of DONE is returned.
        Returns:
        the character at the new position or DONE
      • nextString

        public java.lang.String nextString​(int len)
        Returns the next ascii string of the specified length starting at the current index.
        Parameters:
        len - the length of the string to read
        Returns:
        the next ascii string
      • nextInteger

        public int nextInteger()
        Returns the next integer. The radix must be 10 (decimal). For example, given "...12fred..". If current index is pointing to the '1', then this value will return 12.
        Returns:
        the next base-10 integer.
      • find

        public int find​(char c)
        Looks for the next occurrence of 'c' starting at the current index. Returns the character position in the underlying string or -1 if 'c' is not found.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object