CharacterIterator getIndex() method in Java with Examples

Last Updated : 13 Jul, 2026

The getIndex() method of the CharacterIterator interface in Java is used to retrieve the current position (index) of the iterator. It returns the index of the character currently pointed to by the iterator.

  • Returns the current position of the iterator.
  • Does not modify the iterator's position.
  • Commonly used while traversing characters using CharacterIterator.

Syntax

public int getIndex()

  • Parameter: This method do not accept any parameter.
  • Return Value: The method returns an int representing the current index of the iterator.
  • Exceptions: This method does not throw any exceptions.

Example 1: Getting the Current Index

Java
import java.text.*;
import java.util.*;

public class CharacterIteratorDemo {
    public static void main(String[] args)
    {

        CharacterIterator characterIterator
            = new StringCharacterIterator(
                "GeeksForGeeks");

        System.out.println("Current character: "
                           + characterIterator
                                 .current());

        System.out.println("Current Index: "
                           + characterIterator
                                 .getIndex());
    }
}

Output
Current character: G
Current Index: 0

Explanation: The iterator is initially positioned at the first character ('G'). Therefore, current() returns 'G'. Since the iterator starts from the first character, getIndex() returns 0.

Example 2: Using getIndex() After Moving the Iterator

Java
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class CharacterIteratorDemo {
    public static void main(String[] args) {

        CharacterIterator characterIterator =
                new StringCharacterIterator("GeeksForGeeks");

        characterIterator.next();
        characterIterator.next();
        characterIterator.next();

        System.out.println("Current Character: "
                + characterIterator.current());

        System.out.println("Current Index: "
                + characterIterator.getIndex());
    }
}

Output
Current Character: k
Current Index: 3

Explanation: Initially, the iterator points to index 0. After calling next() three times, it moves to index 3, where the character 'k' is located. Therefore, getIndex() returns 3.

MethodDescription
current()Returns the current character.
getIndex()Returns the current index of the iterator.
first()Moves the iterator to the first character and returns it.
last()Moves the iterator to the last character and returns it.
next()Moves to the next character and returns it.
previous()Moves to the previous character and returns it.

Applications of getIndex()

The getIndex() method is commonly used to:

  • Track the current position while iterating through text.
  • Implement custom text-processing algorithms.
  • Parse strings character by character.
  • Synchronize character positions during text manipulation.
Comment