Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

In this page you can find the example usage for java.lang CharSequence charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:com.aegiswallet.utils.BasicUtils.java

private static String guessAppropriateEncoding(CharSequence contents) {
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }/*from  www  .  j  ava  2s  .c  o  m*/
    }
    return null;
}

From source file:mobile.tiis.appv2.helpers.Utils.java

public static boolean isStringBlank(CharSequence str) {
    int strLen;/*w w  w . ja v  a  2s . com*/
    if (str == null || (strLen = str.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if ((Character.isWhitespace(str.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * <p>Finds the last index in the {@code CharSequence} that matches the
 * specified character.</p>/* w w  w  .  j  av  a  2  s. c  om*/
 *
 * @param cs  the {@code CharSequence} to be processed
 * @param searchChar  the char to be searched for
 * @param start  the start index, negative returns -1, beyond length starts at end
 * @return the index where the search char was found, -1 if not found
 */
static int lastIndexOf(final CharSequence cs, final int searchChar, int start) {
    if (cs instanceof String) {
        return ((String) cs).lastIndexOf(searchChar, start);
    } else {
        final int sz = cs.length();
        if (start < 0) {
            return -1;
        }
        if (start >= sz) {
            start = sz - 1;
        }
        for (int i = start; i >= 0; --i) {
            if (cs.charAt(i) == searchChar) {
                return i;
            }
        }
        return -1;
    }
}

From source file:org.apache.bval.jsr.util.PathNavigation.java

private static String parseQuotedString(CharSequence path, PathPosition pos) throws Exception {
    int len = path.length();
    int start = pos.getIndex();
    if (start < len) {
        char quote = path.charAt(start);
        pos.next();/*from  w  w  w .j a v a 2 s  . c om*/
        StringWriter w = new StringWriter();
        while (pos.getIndex() < len) {
            int here = pos.getIndex();
            // look for matching quote
            if (path.charAt(here) == quote) {
                pos.next();
                return w.toString();
            }
            int codePoints = StringEscapeUtils.UNESCAPE_JAVA.translate(path, here, w);
            if (codePoints == 0) {
                w.write(Character.toChars(Character.codePointAt(path, here)));
                pos.next();
            } else {
                for (int i = 0; i < codePoints; i++) {
                    pos.plus(Character.charCount(Character.codePointAt(path, pos.getIndex())));
                }
            }
        }
        // if reached, reset due to no ending quote found
        pos.setIndex(start);
    }
    return null;
}

From source file:org.apache.bval.jsr.util.PathNavigation.java

private static String parseProperty(CharSequence path, PathPosition pos) throws Exception {
    int len = path.length();
    int start = pos.getIndex();
    loop: while (pos.getIndex() < len) {
        switch (path.charAt(pos.getIndex())) {
        case '[':
        case ']':
        case '.':
            break loop;
        }//from w  w w  . j  ava  2  s. c  om
        pos.next();
    }
    if (pos.getIndex() > start) {
        return path.subSequence(start, pos.getIndex()).toString();
    }
    throw new IllegalStateException(String.format("Position %s: expected property", start));
}

From source file:org.apache.bval.jsr.util.PathNavigation.java

/**
 * Handles an index/key. If the text contained between [] is surrounded by a pair of " or ', it will be treated as a
 * string which may contain Java escape sequences.
 * //  w  ww. j  a  va  2 s. co m
 * @param path
 * @param pos
 * @throws Exception
 */
private static void handleIndex(CharSequence path, PathPosition pos) throws Exception {
    int len = path.length();
    int start = pos.getIndex();
    if (start < len) {
        char first = path.charAt(pos.getIndex());
        if (first == '"' || first == '\'') {
            String s = parseQuotedString(path, pos);
            if (s != null && path.charAt(pos.getIndex()) == ']') {
                pos.handleIndexOrKey(s);
                pos.next();
                return;
            }
        }
        // no quoted string; match ] greedily
        while (pos.getIndex() < len) {
            int here = pos.getIndex();
            try {
                if (path.charAt(here) == ']') {
                    if (here == start) {
                        pos.handleGenericInIterable();
                    } else {
                        pos.handleIndexOrKey(path.subSequence(start, here).toString());
                    }
                    return;
                }
            } finally {
                pos.next();
            }
        }
    }
    throw new IllegalStateException(String.format("Position %s: unparsable index", start));
}

From source file:com.sillelien.dollar.script.DollarLexer.java

public static Parser<?> builtin() {
    return Scanners.pattern(new Pattern() {
        @Override//from  w  w w .ja  v  a2  s . co  m
        public int match(@NotNull CharSequence src, int begin, int end) {
            int i = begin;
            //noinspection StatementWithEmptyBody
            for (; i < end && Character.isAlphabetic(src.charAt(i)); i++) {
                //
            }
            final String name = src.subSequence(begin, i).toString();
            if (Builtins.exists(name)) {
                return i - begin;
            } else {
                return Pattern.MISMATCH;
            }
        }
    }, "builtin").source().map(new Map<String, Tokens.Fragment>() {
        public Tokens.Fragment map(String text) {
            return Tokens.fragment(text, "builtin");
        }

        @NotNull
        @Override
        public String toString() {
            return "builtin";
        }
    });
}

From source file:com.kstenschke.shifter.utils.UtilsTextual.java

/**
 * Get word at caret offset out of given text
 *
 * @param   text           The full text
 * @param   cursorOffset   Character offset of caret
 * @return                 The extracted word or null
 *//*from   w w  w. j a  v a2  s  .co m*/
public static String getWordAtOffset(CharSequence text, int cursorOffset) {
    if (text.length() == 0 || cursorOffset >= text.length())
        return null;

    if (cursorOffset > 0 && !Character.isJavaIdentifierPart(text.charAt(cursorOffset))
            && Character.isJavaIdentifierPart(text.charAt(cursorOffset - 1))) {
        cursorOffset--;
    }

    if (Character.isJavaIdentifierPart(text.charAt(cursorOffset))) {
        int start = cursorOffset;
        int end = cursorOffset;

        while (start > 0 && Character.isJavaIdentifierPart(text.charAt(start - 1))) {
            start--;
        }

        while (end < text.length() && Character.isJavaIdentifierPart(text.charAt(end))) {
            end++;
        }

        return text.subSequence(start, end).toString();
    }

    return null;
}

From source file:org.elasticsearch.hadoop.util.StringUtils.java

public static boolean isLowerCase(CharSequence string) {
    for (int index = 0; index < string.length(); index++) {
        if (Character.isUpperCase(string.charAt(index))) {
            return false;
        }/*from   www  .j a  va 2s . co m*/
    }
    return true;
}

From source file:Main.java

/**
 * Finds the index of the first word that starts with the given prefix.
 * <p>/*from ww  w. j av  a 2  s. c  o  m*/
 * If not found, returns -1.
 *
 * @param text the text in which to search for the prefix
 * @param prefix the text to find, in upper case letters
 */
public static int indexOfWordPrefix(CharSequence text, String prefix) {
    if (prefix == null || text == null) {
        return -1;
    }

    int textLength = text.length();
    int prefixLength = prefix.length();

    if (prefixLength == 0 || textLength < prefixLength) {
        return -1;
    }

    int i = 0;
    while (i < textLength) {
        // Skip non-word characters
        while (i < textLength && !Character.isLetterOrDigit(text.charAt(i))) {
            i++;
        }

        if (i + prefixLength > textLength) {
            return -1;
        }

        // Compare the prefixes
        int j;
        for (j = 0; j < prefixLength; j++) {
            if (Character.toUpperCase(text.charAt(i + j)) != prefix.charAt(j)) {
                break;
            }
        }
        if (j == prefixLength) {
            return i;
        }

        // Skip this word
        while (i < textLength && Character.isLetterOrDigit(text.charAt(i))) {
            i++;
        }
    }

    return -1;
}