How do I iterate a subset of a string?

package org.kodejava.text;

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

public class IterateSubstringExample {
    private static final String text =
        "How razorback-jumping frogs can level six piqued gymnasts";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text, 4, 27, 5);

        // In this loop we just iterator a subset of character defined in the
        // StringCharacterIterator above. It reads from the 4 index of the
        // string up to the 27 character. So it will just take the following
        // string "razorback-jumping frogs"
        for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
            System.out.print(ch);
        }
    }
}

The output of the code snippet above will be:

razorback-jumping frogs

How do I reverse a string using CharacterIterator?

In this example we use the java.text.CharacterIterator implementation class java.text.StringCharacterIterator to reverse a string. It’s done by reading a string from the last index up to the beginning of the string.

package org.kodejava.text;

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

public class StringCharacterIteratorReverseExample {
    private static final String text = "Jackdaws love my big sphinx of quartz";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);

        System.out.println("Before = " + text);
        System.out.print("After  = ");
        // Iterates a string from the last index to the beginning.
        for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
            System.out.print(ch);
        }
    }
}

The result of the code snippet above:

Before = Jackdaws love my big sphinx of quartz
After  = ztrauq fo xnihps gib ym evol swadkcaJ

How do I iterate each character of a string?

The following example show you how to iterate each character of a string using the java.text.CharacterIterator and java.text.StringCharacterIterator to count the number of vowels and consonants in the string.

package org.kodejava.text;

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

public class StringCharacterIteratorExample {
    private static final String text =
        "The quick brown fox jumps over the lazy dog";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);

        int vowels = 0;
        int consonants = 0;

        // Iterates character sets from the beginning to the last character
        for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowels = vowels + 1;
            } else if (ch != ' ') {
                consonants = consonants + 1;
            }
        }

        System.out.println("Number of vowels: " + vowels);
        System.out.println("Number of consonants: " + consonants);
    }
}

The output of the code snippet above:

Number of vowels: 11
Number of consonants: 24