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.clark.func.Functions.java

/**
 * <p>//ww  w  .  ja v a  2 s .c  o  m
 * Checks if the CharSequence contains only unicode letters.
 * </p>
 * 
 * <p>
 * <code>null</code> will return <code>false</code>. An empty CharSequence
 * (length()=0) will return <code>true</code>.
 * </p>
 * 
 * <pre>
 * isAlpha(null)   = false
 * isAlpha("")     = true
 * isAlpha("  ")   = false
 * isAlpha("abc")  = true
 * isAlpha("ab2c") = false
 * isAlpha("ab-c") = false
 * </pre>
 * 
 * @param cs
 *            the CharSequence to check, may be null
 * @return <code>true</code> if only contains letters, and is non-null
 * @since 3.0 Changed signature from isAlpha(String) to
 *        isAlpha(CharSequence)
 */
public static boolean isAlpha(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isLetter(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:com.clark.func.Functions.java

/**
 * <p>// w  w w. ja  v  a  2s. c  o  m
 * Checks if the CharSequence contains only unicode letters or digits.
 * </p>
 * 
 * <p>
 * <code>null</code> will return <code>false</code>. An empty CharSequence
 * (length()=0) will return <code>true</code>.
 * </p>
 * 
 * <pre>
 * isAlphanumeric(null)   = false
 * isAlphanumeric("")     = true
 * isAlphanumeric("  ")   = false
 * isAlphanumeric("abc")  = true
 * isAlphanumeric("ab c") = false
 * isAlphanumeric("ab2c") = true
 * isAlphanumeric("ab-c") = false
 * </pre>
 * 
 * @param cs
 *            the CharSequence to check, may be null
 * @return <code>true</code> if only contains letters or digits, and is
 *         non-null
 * @since 3.0 Changed signature from isAlphanumeric(String) to
 *        isAlphanumeric(CharSequence)
 */
public static boolean isAlphanumeric(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isLetterOrDigit(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from   w w  w . jav a  2  s. co  m*/
 * Capitalizes a String changing the first letter to title case as per
 * {@link Character#toTitleCase(char)}. No other letters are changed.
 * </p>
 * 
 * <p>
 * For a word based algorithm, see
 * {@link org.apache.commons.lang3.text.WordUtils#capitalize(String)}. A
 * <code>null</code> input String returns <code>null</code>.
 * </p>
 * 
 * <pre>
 * capitalize(null)  = null
 * capitalize("")    = ""
 * capitalize("cat") = "Cat"
 * capitalize("cAt") = "CAt"
 * </pre>
 * 
 * @param cs
 *            the String to capitalize, may be null
 * @return the capitalized String, <code>null</code> if null String input
 * @see org.apache.commons.lang3.text.WordUtils#capitalize(String)
 * @see #uncapitalize(CharSequence)
 * @since 2.0
 * @since 3.0 Changed signature from capitalize(String) to
 *        capitalize(CharSequence)
 */
public static String capitalize(CharSequence cs) {
    if (cs == null) {
        return null;
    }
    int strLen;
    if ((strLen = cs.length()) == 0) {
        return cs.toString();
    }
    return new StringBuilder(strLen).append(Character.toTitleCase(cs.charAt(0))).append(subSequence(cs, 1))
            .toString();
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  w ww.j  a va2  s .co m
 * Uncapitalizes a CharSequence changing the first letter to title case as
 * per {@link Character#toLowerCase(char)}. No other letters are changed.
 * </p>
 * 
 * <p>
 * For a word based algorithm, see
 * {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}. A
 * <code>null</code> input String returns <code>null</code>.
 * </p>
 * 
 * <pre>
 * uncapitalize(null)  = null
 * uncapitalize("")    = ""
 * uncapitalize("Cat") = "cat"
 * uncapitalize("CAT") = "cAT"
 * </pre>
 * 
 * @param cs
 *            the String to uncapitalize, may be null
 * @return the uncapitalized String, <code>null</code> if null String input
 * @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
 * @see #capitalize(CharSequence)
 * @since 2.0
 * @since 3.0 Changed signature from uncapitalize(String) to
 *        uncapitalize(CharSequence)
 */
public static String uncapitalize(CharSequence cs) {
    if (cs == null) {
        return null;
    }
    int strLen;
    if ((strLen = cs.length()) == 0) {
        return cs.toString();
    }
    return new StringBuilder(strLen).append(Character.toLowerCase(cs.charAt(0))).append(subSequence(cs, 1))
            .toString();
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from   w w  w  . j  av a2s.com
 * Checks that the CharSequence does not contain certain characters.
 * </p>
 * 
 * <p>
 * A <code>null</code> CharSequence will return <code>true</code>. A
 * <code>null</code> invalid character array will return <code>true</code>.
 * An empty CharSequence (length()=0) always returns true.
 * </p>
 * 
 * <pre>
 * containsNone(null, *)       = true
 * containsNone(*, null)       = true
 * containsNone("", *)         = true
 * containsNone("ab", '')      = true
 * containsNone("abab", 'xyz') = true
 * containsNone("ab1", 'xyz')  = true
 * containsNone("abz", 'xyz')  = false
 * </pre>
 * 
 * @param cs
 *            the CharSequence to check, may be null
 * @param searchChars
 *            an array of invalid chars, may be null
 * @return true if it contains none of the invalid chars, or is null
 * @since 2.0
 * @since 3.0 Changed signature from containsNone(String, char[]) to
 *        containsNone(CharSequence, char[])
 */
public static boolean containsNone(CharSequence cs, char[] searchChars) {
    if (cs == null || searchChars == null) {
        return true;
    }
    int csLen = cs.length();
    int csLast = csLen - 1;
    int searchLen = searchChars.length;
    int searchLast = searchLen - 1;
    for (int i = 0; i < csLen; i++) {
        char ch = cs.charAt(i);
        for (int j = 0; j < searchLen; j++) {
            if (searchChars[j] == ch) {
                if (Character.isHighSurrogate(ch)) {
                    if (j == searchLast) {
                        // missing low surrogate, fine, like
                        // String.indexOf(String)
                        return false;
                    }
                    if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) {
                        return false;
                    }
                } else {
                    // ch is in the Basic Multilingual Plane
                    return false;
                }
            }
        }
    }
    return true;
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  w  w  w .java 2 s  .  c o  m
 * Search a CharSequence to find the first index of any character in the
 * given set of characters.
 * </p>
 * 
 * <p>
 * A <code>null</code> String will return <code>-1</code>. A
 * <code>null</code> or zero length search array will return <code>-1</code>
 * .
 * </p>
 * 
 * <pre>
 * indexOfAny(null, *)                = -1
 * indexOfAny("", *)                  = -1
 * indexOfAny(*, null)                = -1
 * indexOfAny(*, [])                  = -1
 * indexOfAny("zzabyycdxx",['z','a']) = 0
 * indexOfAny("zzabyycdxx",['b','y']) = 3
 * indexOfAny("aba", ['z'])           = -1
 * </pre>
 * 
 * @param cs
 *            the CharSequence to check, may be null
 * @param searchChars
 *            the chars to search for, may be null
 * @return the index of any of the chars, -1 if no match or null input
 * @since 2.0
 * @since 3.0 Changed signature from indexOfAny(String, char[]) to
 *        indexOfAny(CharSequence, char[])
 */
public static int indexOfAny(CharSequence cs, char[] searchChars) {
    if (isEmpty(cs) || isEmpty(searchChars)) {
        return INDEX_NOT_FOUND;
    }
    int csLen = cs.length();
    int csLast = csLen - 1;
    int searchLen = searchChars.length;
    int searchLast = searchLen - 1;
    for (int i = 0; i < csLen; i++) {
        char ch = cs.charAt(i);
        for (int j = 0; j < searchLen; j++) {
            if (searchChars[j] == ch) {
                if (i < csLast && j < searchLast && Character.isHighSurrogate(ch)) {
                    // ch is a supplementary character
                    if (searchChars[j + 1] == cs.charAt(i + 1)) {
                        return i;
                    }
                } else {
                    return i;
                }
            }
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from   w  w w.j  a  va 2s .  c om*/
 * Searches a CharSequence to find the first index of any character not in
 * the given set of characters.
 * </p>
 * 
 * <p>
 * A <code>null</code> CharSequence will return <code>-1</code>. A
 * <code>null</code> or zero length search array will return <code>-1</code>
 * .
 * </p>
 * 
 * <pre>
 * indexOfAnyBut(null, *)           = -1
 * indexOfAnyBut("", *)             = -1
 * indexOfAnyBut(*, null)           = -1
 * indexOfAnyBut(*, [])             = -1
 * indexOfAnyBut("zzabyycdxx",'za') = 3
 * indexOfAnyBut("zzabyycdxx", '')  = 0
 * indexOfAnyBut("aba", 'ab')       = -1
 * </pre>
 * 
 * @param cs
 *            the CharSequence to check, may be null
 * @param searchChars
 *            the chars to search for, may be null
 * @return the index of any of the chars, -1 if no match or null input
 * @since 2.0
 * @since 3.0 Changed signature from indexOfAnyBut(String, char[]) to
 *        indexOfAnyBut(CharSequence, char[])
 */
public static int indexOfAnyBut(CharSequence cs, char[] searchChars) {
    if (isEmpty(cs) || isEmpty(searchChars)) {
        return INDEX_NOT_FOUND;
    }
    int csLen = cs.length();
    int csLast = csLen - 1;
    int searchLen = searchChars.length;
    int searchLast = searchLen - 1;
    outer: for (int i = 0; i < csLen; i++) {
        char ch = cs.charAt(i);
        for (int j = 0; j < searchLen; j++) {
            if (searchChars[j] == ch) {
                if (i < csLast && j < searchLast && Character.isHighSurrogate(ch)) {
                    if (searchChars[j + 1] == cs.charAt(i + 1)) {
                        continue outer;
                    }
                } else {
                    continue outer;
                }
            }
        }
        return i;
    }
    return INDEX_NOT_FOUND;
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  w  w w .  j a  v a2  s  .c  o  m
 * Find the Levenshtein distance between two Strings.
 * </p>
 * 
 * <p>
 * This is the number of changes needed to change one String into another,
 * where each change is a single character modification (deletion, insertion
 * or substitution).
 * </p>
 * 
 * <p>
 * The previous implementation of the Levenshtein distance algorithm was
 * from <a
 * href="http://www.merriampark.com/ld.htm">http://www.merriampark.com
 * /ld.htm</a>
 * </p>
 * 
 * <p>
 * Chas Emerick has written an implementation in Java, which avoids an
 * OutOfMemoryError which can occur when my Java implementation is used with
 * very large strings.<br>
 * This implementation of the Levenshtein distance algorithm is from <a
 * href="http://www.merriampark.com/ldjava.htm">http://www.merriampark.com/
 * ldjava.htm</a>
 * </p>
 * 
 * <pre>
 * getLevenshteinDistance(null, *)             = IllegalArgumentException
 * getLevenshteinDistance(*, null)             = IllegalArgumentException
 * getLevenshteinDistance("","")               = 0
 * getLevenshteinDistance("","a")              = 1
 * getLevenshteinDistance("aaapppp", "")       = 7
 * getLevenshteinDistance("frog", "fog")       = 1
 * getLevenshteinDistance("fly", "ant")        = 3
 * getLevenshteinDistance("elephant", "hippo") = 7
 * getLevenshteinDistance("hippo", "elephant") = 7
 * getLevenshteinDistance("hippo", "zzzzzzzz") = 8
 * getLevenshteinDistance("hello", "hallo")    = 1
 * </pre>
 * 
 * @param s
 *            the first String, must not be null
 * @param t
 *            the second String, must not be null
 * @return result distance
 * @throws IllegalArgumentException
 *             if either String input <code>null</code>
 * @since 3.0 Changed signature from getLevenshteinDistance(String, String)
 *        to getLevenshteinDistance(CharSequence, CharSequence)
 */
public static int getLevenshteinDistance(CharSequence s, CharSequence t) {
    if (s == null || t == null) {
        throw new IllegalArgumentException("Strings must not be null");
    }

    /*
     * The difference between this impl. and the previous is that, rather
     * than creating and retaining a matrix of size s.length()+1 by
     * t.length()+1, we maintain two single-dimensional arrays of length
     * s.length()+1. The first, d, is the 'current working' distance array
     * that maintains the newest distance cost counts as we iterate through
     * the characters of String s. Each time we increment the index of
     * String t we are comparing, d is copied to p, the second int[]. Doing
     * so allows us to retain the previous cost counts as required by the
     * algorithm (taking the minimum of the cost count to the left, up one,
     * and diagonally up and to the left of the current cost count being
     * calculated). (Note that the arrays aren't really copied anymore, just
     * switched...this is clearly much better than cloning an array or doing
     * a System.arraycopy() each time through the outer loop.)
     * 
     * Effectively, the difference between the two implementations is this
     * one does not cause an out of memory condition when calculating the LD
     * over two very large strings.
     */

    int n = s.length(); // length of s
    int m = t.length(); // length of t

    if (n == 0) {
        return m;
    } else if (m == 0) {
        return n;
    }

    if (n > m) {
        // swap the input strings to consume less memory
        CharSequence tmp = s;
        s = t;
        t = tmp;
        n = m;
        m = t.length();
    }

    int p[] = new int[n + 1]; // 'previous' cost array, horizontally
    int d[] = new int[n + 1]; // cost array, horizontally
    int _d[]; // placeholder to assist in swapping p and d

    // indexes into strings s and t
    int i; // iterates through s
    int j; // iterates through t

    char t_j; // jth character of t

    int cost; // cost

    for (i = 0; i <= n; i++) {
        p[i] = i;
    }

    for (j = 1; j <= m; j++) {
        t_j = t.charAt(j - 1);
        d[0] = j;

        for (i = 1; i <= n; i++) {
            cost = s.charAt(i - 1) == t_j ? 0 : 1;
            // minimum of cell to the left+1, to the top+1, diagonally left
            // and up +cost
            d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
        }

        // copy current distance counts to 'previous row' distance counts
        _d = p;
        p = d;
        d = _d;
    }

    // our last action in the above loop was to switch d and p, so p now
    // actually has the most recent cost counts
    return p[n];
}