How do I reverse a string using CharacterIterator?

Category: java.text, viewed: 779 time(s).

In this example we use the CharacterIterator implementation class 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.example.text;
 
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
 
public class ReverseStringCharacterExample {
    private static final String text = "Jackdaws love my big sphinx of quartz";
 
    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);
 
        //
        // 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);
        }
    }
}
 
 
Bookmark this example!  

Most Viewed Examples

Google

100 Top & Latest


eXTReMe Tracker
visitor stats