Example usage for java.text StringCharacterIterator last

List of usage examples for java.text StringCharacterIterator last

Introduction

In this page you can find the example usage for java.text StringCharacterIterator last.

Prototype

public char last() 

Source Link

Document

Implements CharacterIterator.last() for String.

Usage

From source file:Util.java

/**
* Returns a string that contains all characters of the given string in
* reverse order.//w  ww . ja va2 s  .c  o m
*
* @param str
*
* @return
*/
public String reverse(String str) {
    if (str == null) {
        return null;
    }

    char[] newStr = new char[str.length()];
    StringCharacterIterator iterator = new StringCharacterIterator(str);
    int i = 0;

    for (char ch = iterator.last(); ch != CharacterIterator.DONE; ch = iterator.previous()) {
        newStr[i] = ch;
        i++;
    }

    return new String(newStr);
}