Example usage for java.text StringCharacterIterator previous

List of usage examples for java.text StringCharacterIterator previous

Introduction

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

Prototype

public char previous() 

Source Link

Document

Implements CharacterIterator.previous() for String.

Usage

From source file:Util.java

/**
* Returns a string that contains all characters of the given string in
* reverse order.//  www .  j  av  a2 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);
}

From source file:ar.com.tadp.xml.rinzo.core.model.XMLNode.java

/**
 * Devuelve el String sobre el que est posicionado el cursor
 *///from w w  w . j a  v a2 s .c  om
public String getStringAt(int offset) {
    int relativeOffset = offset - this.offset;
    int start = 0, end = 0;
    String content = this.getContent();
    StringCharacterIterator iter = new StringCharacterIterator(content);
    char c;

    for (c = iter.setIndex(relativeOffset); c != CharacterIterator.DONE
            && this.isFullIdentifierPart(c); c = iter.previous()) {
    }
    start = this.isFullIdentifierPart(iter.current()) ? iter.getIndex() : iter.getIndex() + 1;

    for (c = iter.setIndex(relativeOffset); c != CharacterIterator.DONE
            && this.isFullIdentifierPart(c); c = iter.next()) {
    }
    end = iter.getIndex();

    return (start <= end) ? content.substring(start, end) : "";
}

From source file:HexFormat.java

/**
 * Parse a hexadecimal number, skipping leading whitespace. Does not throw
 * an exception; if no object can be parsed, index is unchanged! Hexadecimal
 * numbers may be indicated with a leading character designation of '0x'.
 * /* w w  w . j a  va2s.  c o  m*/
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The hexadecimal number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    StringBuffer buffer = new StringBuffer();
    char c, c2;
    long result;

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        break;
    }

    if (c == CharacterIterator.DONE) {
        return (null);
    }

    if (c == '0') {
        c2 = iter.next();

        if (c2 == CharacterIterator.DONE) {
            return (null);
        }

        if (c2 == 'x') {
            // has a leading '0x' designation, so skip over it
        } else {
            // replace the two characters
            iter.previous();
            iter.previous();
        }
    } else {
        // skip back one character
        iter.previous();
    }

    // gather valid hex digits
    for (c = iter.next(); c != CharacterIterator.DONE; c = iter.next()) {
        if (hexDigits.indexOf(c) != -1) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        // no valid hex digits
        return (null);
    }

    // convert hex to long
    if (buffer.length() > 16) {
        // larger than a long, error
        // with a buffer full of nibbles, the maximum nibbles in a
        // 64 bit number is 16 nibbles
        return (null);
    }

    // parse number
    try {
        result = Long.parseLong(buffer.toString(), 16);
    } catch (NumberFormatException e) {
        // unable to parse number
        return (null);
    }

    status.setIndex(iter.getIndex());
    return (new Long(result));
}