Example usage for java.text CharacterIterator previous

List of usage examples for java.text CharacterIterator previous

Introduction

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

Prototype

public char previous();

Source Link

Document

Decrements the iterator's index by one and returns the character at the new index.

Usage

From source file:Main.java

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

    for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
        System.out.print(ch);/* ww  w.jav a2  s  . c om*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CharacterIterator it = new StringCharacterIterator("abcd");

    for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) {
        System.out.println(ch);//w w  w  .j  ava  2s.  c o m
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CharacterIterator it = new StringCharacterIterator("abcd");

    char ch = it.first();
    ch = it.current();/*w ww .ja v  a  2s .  c  om*/
    ch = it.next();
    ch = it.current();
    ch = it.last();
    int pos = it.getIndex();
    ch = it.next();
    pos = it.getIndex();
    ch = it.previous();
    ch = it.setIndex(1);
}

From source file:com.woonoz.proxy.servlet.UrlRewriterImpl.java

private static String removeTrailingSlashes(final String text) {
    if (text.isEmpty()) {
        return text;
    }//  www.j  ava2s .com
    final CharacterIterator it = new StringCharacterIterator(text);
    Character c = it.last();
    while (c.equals('/')) {
        c = it.previous();
    }
    return text.substring(0, it.getIndex() + 1);
}

From source file:org.apache.arrow.vector.util.Text.java

/**
 * For the given string, returns the number of UTF-8 bytes required to encode the string.
 *
 * @param string//from  w ww  . ja va2s  .  com
 *          text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                iter.previous(); // rewind one
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}

From source file:net.issarlk.androbunny.inkbunny.API.java

public static String extractTree(Pattern pattern, String data) {
    Matcher matches = pattern.matcher(data);
    if (matches.matches()) {
        data = matches.group(1);//  w  w  w . j  a v  a2  s .  c  o m
        StringBuilder result = new StringBuilder();
        //Extract
        int taglevel = 0;

        CharacterIterator it = new StringCharacterIterator(data);
        int mode = 0;
        int inparamname = 0;
        int inparamvalue = 0;
        int outsidetags = 0;
        int intagname = 0;
        char paramvaluechar = '"';
        HashMap<String, String> params = new HashMap<String, String>();
        StringBuilder tagname = new StringBuilder();
        StringBuilder paramname = new StringBuilder();
        StringBuilder paramvalue = new StringBuilder();
        StringBuilder text = new StringBuilder();
        for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {

            if (c == '<') {
                //start of tag
                c = it.next();
                params.clear();
                outsidetags = 0;
                //Output text
                //Log.d(TAG, repeat(' ', taglevel) + text.toString());
                result.append(text);
                if (c == '/') {
                    //Start of closing tag
                    mode = 3;
                    intagname = 1;
                    c = it.next();
                } else {
                    //Start of openning tag
                    mode = 2;
                    intagname = 1;
                }
                tagname = new StringBuilder();
            }
            if (intagname == 1) {
                if (c < 'a' || c > 'z') {
                    intagname = 0;
                } else {
                    tagname.append(c);
                }
            }
            if ((mode == 2 || mode == 3) && intagname == 0 && inparamname == 0 && inparamvalue == 0 && c >= 'a'
                    && c <= 'z') {
                inparamname = 1;
                paramname = new StringBuilder();
            }
            if (inparamname == 1) {
                if (c == '=') {
                    inparamname = 0;
                    inparamvalue = 1;
                    paramvaluechar = it.next();
                    paramvalue = new StringBuilder();
                    continue;
                } else if (c != ' ') {
                    paramname.append(c);
                }
            }
            if (inparamvalue == 1) {
                if (c == paramvaluechar) {
                    //End of value
                    inparamvalue = 0;
                    params.put(paramname.toString(), paramvalue.toString());
                } else {
                    paramvalue.append(c);
                }
            }
            if (mode == 2 && inparamvalue == 0 && c == '/') {
                c = it.next();
                if (c == '>') {
                    //tag closed at its end
                    mode = 4;
                    if (tagname.toString().equals("br")) {
                        result.append("<br/>");
                    }
                } else {
                    c = it.previous();
                }
            }
            if (c == '>') {
                if (mode == 2) {
                    //openning
                    taglevel += 1;
                    outsidetags = 1;
                    text = new StringBuilder();
                    String paramstr = "";
                    for (String key : params.keySet()) {
                        paramstr += " " + key + "=\"" + params.get(key) + "\"";
                    }
                    //Log.d(TAG, repeat(' ', taglevel - 1) + "<" + tagname.toString() + paramstr + ">");
                    result.append("<" + tagname.toString() + paramstr + ">");
                    mode = 0;
                    continue;
                } else if (mode == 3) {
                    //Closing
                    taglevel -= 1;
                    outsidetags = 1;
                    text = new StringBuilder();
                    //Log.d(TAG, repeat(' ', taglevel) + "</" + tagname.toString() + ">");
                    result.append("</" + tagname.toString() + ">\n");
                    mode = 0;
                    if (taglevel <= 0) {
                        break;
                    }
                    continue;
                } else if (mode == 4) {
                    //closed at end
                    outsidetags = 1;
                    text = new StringBuilder();
                    //Log.d(TAG, repeat(' ', taglevel) + "<" + tagname.toString() + "/>");
                    result.append("<" + tagname.toString() + "/>\n");
                    mode = 0;
                    continue;
                }
            }
            if (outsidetags == 1) {
                //Append char
                text.append(c);
            }
        }

        String res = result.toString();
        return res;
    }
    return null;
}