Class StringCharacterIterator in Java

digitization, transformation, binary

Class StringCharacterIterator in Java

The StringCharacterIterator class can move bi-directionally through a character string. In other words, the class iterates through the characters in a String. The class implements the CharacterIterator interface. The class is used by BreakIterator to find boundaries in text strings.

The structure of the class StringCharacterIterator is given by

public final class java.text.StringCharacterIterator extends java.lang.Object implements java.text.CharacterIterator {
// Constructors
public StringCharacterIterator(String text);
public StringCharacterIterator(String text, int pos);
public StringCharacterIterator(String text, int begin, int end, int pos);
// Instance Methods
public Object clone();
public char current();
public boolean equals(Object obj);
public char first();
public int getBeginIndex();
public int getEndIndex();
public int getIndex();
public int hashCode();
public char last();
public char next();
public char previous();
public char setIndex(int p);
}

The details of the class structure are given as follows:

Ads code goes here
public StringCharacterIterator(String text);

public StringCharacterIterator(String text) constructor creates a StringCharacterIterator that uses the given string. The initial index of the iterator is at the beginning of the string, or in other words, at index 0.

Parameter
text – The String to use.

public StringCharacterIterator(String text, int pos);

public StringCharacterIterator(String text, int pos) constructor creates a StringCharacterIterator that uses the given string. The initial index of the iterator is set to the given initial position.

Parameter
text – The String to use.
pos – The initial position.

public StringCharacterIterator(String text, int begin, int end, int pos);

public StringCharacterIterator(String text, int begin, int end, int pos) constructor creates a StringCharacterIterator that uses the specified range of the given string. In other words, the iterator uses the sequence of text from the specified beginning index to the specified ending index. The initial index of the iterator is set to the given initial position.

READ  Abstract interface ObjectInputValidation in Java

Parameter
text – The String to use.
begin – The beginning index.
end – The ending index.
pos – The initial position.

public Object clone();

public Object clone() method creates a copy of this StringCharacterIterator and returns it.

This method returns a copy of this StringCharacterIterator.

public char current();

public char current() method returns the character at the current position of this CharacterIterator. The current
position is returned by getIndex().

This method returns the character at the current position of this StringCharacterIterator or DONE if the current
the position is not within the text sequence.

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of StringCharacterIterator and is equivalent to this StringCharacterIterator.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public char first();

public char first() method returns the character at the first position in this StringCharacterIterator. The first
position is returned by getBeginIndex(). The current position of the iterator is set to this position.

This method returns the first character in this StringCharacterIterator.

public int getBeginIndex();

public int getBeginIndex() method returns the index of the beginning of the text for this StringCharacterIterator.

This method returns the index of the first character in this StringCharacterIterator.

public int getEndIndex();

public int getEndIndex() method returns the index of the character following the end of the text for this
StringCharacterIterator.

This method returns the index after the last character in this StringCharacterIterator.

public int getIndex();

public int getIndex() method returns the current position, or index, of this StringCharacterIterator.

READ  What is JavaBeans?

This method returns the index of the current character in this StringCharacterIterator.

public int hashCode();

public int hashCode() method returns a hashcode for this StringCharacterIterator.

This method returns a hashcode for this object.

public char last();

public char last() method returns the character at the ending position of this StringCharacterIterator. The last position is the value of getEndIndex()-1. The current position of the iterator is set to this position.

This method returns the last character in this StringCharacterIterator.

public char next();

public char next() method increments the current position of this StringCharacterIterator by 1 and returns the character at the new position. If the current position is already at getEndIndex(), the position is not changed and DONE is returned.

This method returns the next character in this StringCharacterIterator or DONE if the current position is already at
the end of the text.

public char previous();

public char previous() method decrements the current position of this StringCharacterIterator by 1 and returns the character at the new position. If the current position is already at getBeginIndex(), the position is not changed and DONE is returned.

This method returns the previous character in this StringCharacterIterator or DONE if the current position is
already at the beginning of the text.

public char setIndex(int p);

public char setIndex(int p) method sets the current position, or index, of this StringCharacterIterator to the given
position.

This method returns the character at the specified position in this StringCharacterIterator.

Parameter
p – The new position.

Apart from these StringCharacterIterator class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)
READ  Class SimpleDateFormat in Java

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *