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:Main.java

/**
 * NOTE: No well-formness check for the referenced char.
 *
 * @param start Index to char after "&#".
 *///  w w  w  . j  a  v a 2  s  . c  o  m
public static boolean isXmlCharRefPart(CharSequence str, int start) {
    int len = str.length();
    if (start >= len)
        return false;
    char c;
    if (str.charAt(start) == 'x') {
        // &#xhex;
        ++start;
        int i = start;
        for (; i < len; ++i) {
            c = str.charAt(i);
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
                continue;
            break;
        }
        return (i > start && i < len && str.charAt(i) == ';');
    }
    // &#dec;
    int i = start;
    for (; i < len; ++i) {
        c = str.charAt(i);
        if (c < '0' || c > '9')
            break;
    }
    return (i > start && i < len && str.charAt(i) == ';');
}

From source file:Main.java

public static CharSequence trimTrailingWhitespace(CharSequence source) {

    if (source == null)
        return "";

    int i = source.length();

    // loop back to the first non-whitespace character
    while (--i >= 0 && Character.isWhitespace(source.charAt(i))) {
    }/*w w  w  .j av a2 s . c o m*/

    return source.subSequence(0, i + 1);
}

From source file:Main.java

/**
 * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
 *
 * <pre>//from  w  w  w  .  j  av a2s. c o  m
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Gets the index of the longest NCName that is the suffix of a character
 * sequence.//from www  .java2 s .c  o  m
 * 
 * @param s
 *        The character sequence.
 * @return The index of the longest suffix of the specified character
 *         sequence {@code s} that is an NCName, or -1 if the character
 *         sequence {@code s} does not have a suffix that is an NCName.
 */
public static int getNCNameSuffixIndex(CharSequence s) {
    // identify bnode labels and do not try to split them
    if (s.length() > 1 && s.charAt(0) == '_' && s.charAt(1) == ':') {
        return -1;
    }
    int index = -1;
    for (int i = s.length() - 1; i > -1; i--) {
        if (!Character.isLowSurrogate(s.charAt(i))) {
            int codePoint = Character.codePointAt(s, i);
            if (isNCNameStartChar(codePoint)) {
                index = i;
            }
            if (!isNCNameChar(codePoint)) {
                break;
            }
        }
    }
    return index;
}

From source file:Main.java

/**
 * Gets the index of the longest NCName that is the suffix of a character
 * sequence.// w w  w.j  a v  a 2  s  .  com
 * 
 * @param cs
 *           The character sequence.
 * @return Returns the index of the longest suffix of the specified character
 *         sequence <code>cs</code> that is an NCName, or -1 if the character
 *         sequence <code>cs</code> does not have a suffix that is an NCName.
 */
public static int getNCNameSuffixIndex(CharSequence cs) {
    int index = -1;
    for (int i = cs.length() - 1; i > -1; i--) {
        if (!Character.isLowSurrogate(cs.charAt(i))) {
            int c = Character.codePointAt(cs, i);
            if (isNCNameStartChar(c)) {
                index = i;
            }
            if (!isNCNameChar(c)) {
                break;
            }
        }
    }
    return index;
}

From source file:Strings.java

/**
 * Returns a hash code for a character sequence that is equivalent
 * to the hash code generated for a its string yield.  Recall that
 * the interface {@link CharSequence} does not refine the definition
 * of equality beyond that of {@link Object#equals(Object)}.
 *
 * <P>The return result is the same as would be produced by:
 *
 * <pre>/* ww  w  . j a va  2s .  com*/
 *    hashCode(cSeq) = cSeq.toString().hashCode()</pre>
 *
 * Recall that the {@link CharSequence} interface requires its
 * {@link CharSequence#toString()} to return a string
 * corresponding to its characters as returned by
 * <code>charAt(0),...,charAt(length()-1)</code>.  This value
 * can be defined directly by inspecting the hash code for strings:
 *
 * <pre>
 *      int h = 0;
 *      for (int i = 0; i < cSeq.length(); ++i)
 *          h = 31*h + cSeq.charAt(i);
 *      return h;</pre>
 *
 * @param cSeq The character sequence.
 * @return The hash code for the specified character sequence.
 */
public static int hashCode(CharSequence cSeq) {
    if (cSeq instanceof String)
        return cSeq.hashCode();
    int h = 0;
    for (int i = 0; i < cSeq.length(); ++i)
        h = 31 * h + cSeq.charAt(i);
    return h;
}

From source file:Main.java

public static void escAttrValue(StringBuilder ret, CharSequence value, char delim) {
    for (int i = 0, len = value.length(); i < len; i++) {
        char c = value.charAt(i);
        switch (c) {
        case '&':
            if (isXmlEntityRef(value, i))
                ret.append(c);//from w  ww  . j av a2s  . c  om
            else
                ret.append("&amp;");
            break;
        case '<':
            ret.append("&lt;");
            break;
        case '\'':
            if (c == delim)
                ret.append("&apos;");
            else
                ret.append(c);
            break;
        case '"':
            if (c == delim)
                ret.append("&quot;");
            else
                ret.append(c);
            break;
        default:
            ret.append(c);
        }
    }
}

From source file:Main.java

/**
 * Check whether the given CharSequence contains any whitespace characters.
 *
 * @param seq the CharSequence to check (may be {@code null})
 * @return {@code true} if the CharSequence is not empty and
 * contains at least 1 whitespace character
 * @see java.lang.Character#isWhitespace
 * @since 3.0//w  ww.  j  av a 2  s  .com
 */
// From org.springframework.util.StringUtils, under Apache License 2.0
public static boolean containsWhitespace(final CharSequence seq) {
    if (isEmpty(seq)) {
        return false;
    }
    final int strLen = seq.length();
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(seq.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
 * <p>// w w  w.ja v  a  2s . c om
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param cs the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(final CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:snow.utils.MDigest.java

public static String skein512(CharSequence chars) {

    byte[] digest = new byte[512];

    byte[] msg = new byte[chars.length()];
    for (int i = 0; i < chars.length(); i++) {
        msg[i] = (byte) chars.charAt(i);
    }/*w w  w.j  a  v  a2  s .  c  om*/

    Skein512.hash(msg, digest);

    return Base64.encodeBase64String(digest);
}