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 new StringCharacterIterator on the specified string.
    StringCharacterIterator​(String value, int location)
    Constructs a new StringCharacterIterator on the specified string with the current index set to the specified value.
    StringCharacterIterator​(String value, int start, int end, int location)
    Constructs a new StringCharacterIterator on the specified string with the begin, end and current index set to the specified values.
  • Method Summary

    Modifier and Type Method Description
    Object clone()
    Returns a new StringCharacterIterator with the same source string, begin, end, and current index as this iterator.
    char current()
    Returns the character at the current index in the source string.
    boolean equals​(Object object)
    Compares the specified object with this StringCharacterIterator and indicates if they are equal.
    char first()
    Sets the current position to the begin index and returns the character at the new position in the source string.
    int getBeginIndex()
    Returns the begin index in the source string.
    int getEndIndex()
    Returns the end index in the source string.
    int getIndex()
    Returns the current index in the source string.
    int hashCode()
    Returns an integer hash code for this object.
    char last()
    Sets the current position to the end index - 1 and returns the character at the new position.
    char next()
    Increments the current index and returns the character at the new index.
    char previous()
    Decrements the current index and returns the character at the new index.
    char setIndex​(int location)
    Sets the current index in the source string.
    void setText​(String value)
    Sets the source string to iterate over.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StringCharacterIterator

      public StringCharacterIterator​(String value)
      Constructs a new StringCharacterIterator on 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

      public StringCharacterIterator​(String value, int location)
      Constructs a new StringCharacterIterator on 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 - if location is negative or greater than the length of the source string.
    • StringCharacterIterator

      public StringCharacterIterator​(String value, int start, int end, int location)
      Constructs a new StringCharacterIterator on 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 - if start < 0, start > end, location < start, location > end or if end is greater than the length of value.
  • Method Details

    • clone

      public Object clone()
      Returns a new StringCharacterIterator with the same source string, begin, end, and current index as this iterator.
      Specified by:
      clone in interface CharacterIterator
      Overrides:
      clone in class Object
      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:
      current in interface CharacterIterator
      Returns:
      the current character, or DONE if the current index is past the end.
    • equals

      public boolean equals​(Object object)
      Compares the specified object with this StringCharacterIterator and indicates if they are equal. In order to be equal, object must be an instance of StringCharacterIterator that iterates over the same sequence of characters with the same index.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare with this object.
      Returns:
      true if the specified object is equal to this StringCharacterIterator; false otherwise.
      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:
      first in interface CharacterIterator
      Returns:
      the character at the begin index or DONE if the begin index is equal to the end index.
    • getBeginIndex

      public int getBeginIndex()
      Returns the begin index in the source string.
      Specified by:
      getBeginIndex in interface CharacterIterator
      Returns:
      the index of the first character of the iteration.
    • getEndIndex

      public int getEndIndex()
      Returns the end index in the source string.
      Specified by:
      getEndIndex in interface CharacterIterator
      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:
      getIndex in interface CharacterIterator
      Returns:
      the current index.
    • hashCode

      public int hashCode()
      Description copied from class: Object
      Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually 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 hashCode method if you intend implementing your own hashCode method.

      Overrides:
      hashCode in class Object
      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:
      last in interface CharacterIterator
      Returns:
      the character before the end index or DONE if 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:
      next in interface CharacterIterator
      Returns:
      the character at the next index, or DONE if 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:
      previous in interface CharacterIterator
      Returns:
      the character at the previous index, or DONE if the previous index would be past the beginning.
    • setIndex

      public char setIndex​(int location)
      Sets the current index in the source string.
      Specified by:
      setIndex in interface CharacterIterator
      Parameters:
      location - the index the current position is set to.
      Returns:
      the character at the new index, or DONE if location is set to the end index.
      Throws:
      IllegalArgumentException - if location is smaller than the begin index or greater than the end index.
    • setText

      public void setText​(String value)
      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.