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:bfile.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode digits.
 * A decimal point is not a Unicode digit and returns false.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code false}.</p>
 *
 * <p>Note that the method does not allow for a leading sign, either positive or negative.
 * Also, if a String passes the numeric test, it may still generate a NumberFormatException
 * when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range
 * for int or long respectively.</p>
 *
 * <pre>//from   ww  w  .j  a va 2  s.c om
 * StringUtils.isNumeric(null)   = false
 * StringUtils.isNumeric("")     = false
 * StringUtils.isNumeric("  ")   = false
 * StringUtils.isNumeric("123")  = true
 * StringUtils.isNumeric("\u0967\u0968\u0969")  = true
 * StringUtils.isNumeric("12 3") = false
 * StringUtils.isNumeric("ab2c") = false
 * StringUtils.isNumeric("12-3") = false
 * StringUtils.isNumeric("12.3") = false
 * StringUtils.isNumeric("-123") = false
 * StringUtils.isNumeric("+123") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains digits, and is non-null
 * @since 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence)
 * @since 3.0 Changed "" to return false and not true
 */
public static boolean isNumeric(final CharSequence cs) {
    if (isEmpty(cs)) {
        return false;
    }
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (!Character.isDigit(cs.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:bfile.util.StringUtils.java

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

From source file:bfile.util.StringUtils.java

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

From source file:bfile.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode letters or digits.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code false}.</p>
 *
 * <pre>//from www  .j  a  va 2  s  .  c om
 * StringUtils.isAlphanumeric(null)   = false
 * StringUtils.isAlphanumeric("")     = false
 * StringUtils.isAlphanumeric("  ")   = false
 * StringUtils.isAlphanumeric("abc")  = true
 * StringUtils.isAlphanumeric("ab c") = false
 * StringUtils.isAlphanumeric("ab2c") = true
 * StringUtils.isAlphanumeric("ab-c") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains letters or digits,
 *  and is non-null
 * @since 3.0 Changed signature from isAlphanumeric(String) to isAlphanumeric(CharSequence)
 * @since 3.0 Changed "" to return false and not true
 */
public static boolean isAlphanumeric(final CharSequence cs) {
    if (isEmpty(cs)) {
        return false;
    }
    final 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:bfile.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only ASCII printable characters.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>//from ww  w  .  jav a 2 s.  c  o m
 * StringUtils.isAsciiPrintable(null)     = false
 * StringUtils.isAsciiPrintable("")       = true
 * StringUtils.isAsciiPrintable(" ")      = true
 * StringUtils.isAsciiPrintable("Ceki")   = true
 * StringUtils.isAsciiPrintable("ab2c")   = true
 * StringUtils.isAsciiPrintable("!ab-c~") = true
 * StringUtils.isAsciiPrintable("\u0020") = true
 * StringUtils.isAsciiPrintable("\u0021") = true
 * StringUtils.isAsciiPrintable("\u007e") = true
 * StringUtils.isAsciiPrintable("\u007f") = false
 * StringUtils.isAsciiPrintable("Ceki G\u00fclc\u00fc") = false
 * </pre>
 *
 * @param cs the CharSequence to check, may be null
 * @return {@code true} if every character is in the range
 *  32 thru 126
 * @since 2.1
 * @since 3.0 Changed signature from isAsciiPrintable(String) to isAsciiPrintable(CharSequence)
 */
public static boolean isAsciiPrintable(final CharSequence cs) {
    if (cs == null) {
        return false;
    }
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (CharUtils.isAsciiPrintable(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only lowercase characters.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code false}.</p>
 *
 * <pre>/*w  w  w  .jav a  2  s  .c o m*/
 * StringUtils.isAllLowerCase(null)   = false
 * StringUtils.isAllLowerCase("")     = false
 * StringUtils.isAllLowerCase("  ")   = false
 * StringUtils.isAllLowerCase("abc")  = true
 * StringUtils.isAllLowerCase("abC")  = false
 * StringUtils.isAllLowerCase("ab c") = false
 * StringUtils.isAllLowerCase("ab1c") = false
 * StringUtils.isAllLowerCase("ab/c") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains lowercase characters, and is non-null
 * @since 2.5
 * @since 3.0 Changed signature from isAllLowerCase(String) to isAllLowerCase(CharSequence)
 */
public static boolean isAllLowerCase(final CharSequence cs) {
    if (cs == null || isEmpty(cs)) {
        return false;
    }
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isLowerCase(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only uppercase characters.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty String (length()=0) will return {@code false}.</p>
 *
 * <pre>/*from  www .j a v a  2  s. c om*/
 * StringUtils.isAllUpperCase(null)   = false
 * StringUtils.isAllUpperCase("")     = false
 * StringUtils.isAllUpperCase("  ")   = false
 * StringUtils.isAllUpperCase("ABC")  = true
 * StringUtils.isAllUpperCase("aBC")  = false
 * StringUtils.isAllUpperCase("A C")  = false
 * StringUtils.isAllUpperCase("A1C")  = false
 * StringUtils.isAllUpperCase("A/C")  = false
 * </pre>
 *
 * @param cs the CharSequence to check, may be null
 * @return {@code true} if only contains uppercase characters, and is non-null
 * @since 2.5
 * @since 3.0 Changed signature from isAllUpperCase(String) to isAllUpperCase(CharSequence)
 */
public static boolean isAllUpperCase(final CharSequence cs) {
    if (cs == null || isEmpty(cs)) {
        return false;
    }
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isUpperCase(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Search a CharSequence to find the first index of any
 * character not in the given set of characters.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A {@code null} or empty search string will return {@code -1}.</p>
 *
 * <pre>/*from  w  w  w. j  a v a 2 s .c  o m*/
 * StringUtils.indexOfAnyBut(null, *)            = -1
 * StringUtils.indexOfAnyBut("", *)              = -1
 * StringUtils.indexOfAnyBut(*, null)            = -1
 * StringUtils.indexOfAnyBut(*, "")              = -1
 * StringUtils.indexOfAnyBut("zzabyycdxx", "za") = 3
 * StringUtils.indexOfAnyBut("zzabyycdxx", "")   = -1
 * StringUtils.indexOfAnyBut("aba","ab")         = -1
 * </pre>
 *
 * @param seq  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, String) to indexOfAnyBut(CharSequence, CharSequence)
 */
public static int indexOfAnyBut(final CharSequence seq, final CharSequence searchChars) {
    if (isEmpty(seq) || isEmpty(searchChars)) {
        return INDEX_NOT_FOUND;
    }
    final int strLen = seq.length();
    for (int i = 0; i < strLen; i++) {
        final char ch = seq.charAt(i);
        final boolean chFound = CharSequenceUtils.indexOf(searchChars, ch, 0) >= 0;
        if (i + 1 < strLen && Character.isHighSurrogate(ch)) {
            final char ch2 = seq.charAt(i + 1);
            if (chFound && CharSequenceUtils.indexOf(searchChars, ch2, 0) < 0) {
                return i;
            }
        } else {
            if (!chFound) {
                return i;
            }
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Compares two CharSequences, and returns the index at which the
 * CharSequences begin to differ.</p>
 *
 * <p>For example,/*from   ww w.j a  va  2 s .  c  o m*/
 * {@code indexOfDifference("i am a machine", "i am a robot") -> 7}</p>
 *
 * <pre>
 * StringUtils.indexOfDifference(null, null) = -1
 * StringUtils.indexOfDifference("", "") = -1
 * StringUtils.indexOfDifference("", "abc") = 0
 * StringUtils.indexOfDifference("abc", "") = 0
 * StringUtils.indexOfDifference("abc", "abc") = -1
 * StringUtils.indexOfDifference("ab", "abxyz") = 2
 * StringUtils.indexOfDifference("abcde", "abxyz") = 2
 * StringUtils.indexOfDifference("abcde", "xyz") = 0
 * </pre>
 *
 * @param cs1  the first CharSequence, may be null
 * @param cs2  the second CharSequence, may be null
 * @return the index where cs1 and cs2 begin to differ; -1 if they are equal
 * @since 2.0
 * @since 3.0 Changed signature from indexOfDifference(String, String) to
 * indexOfDifference(CharSequence, CharSequence)
 */
public static int indexOfDifference(final CharSequence cs1, final CharSequence cs2) {
    if (cs1 == cs2) {
        return INDEX_NOT_FOUND;
    }
    if (cs1 == null || cs2 == null) {
        return 0;
    }
    int i;
    for (i = 0; i < cs1.length() && i < cs2.length(); ++i) {
        if (cs1.charAt(i) != cs2.charAt(i)) {
            break;
        }
    }
    if (i < cs2.length() || i < cs1.length()) {
        return i;
    }
    return INDEX_NOT_FOUND;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Counts how many times the char appears in the given string.</p>
 *
 * <p>A {@code null} or empty ("") String input returns {@code 0}.</p>
 *
 * <pre>//from  w  w w .  j av a  2 s  .  c om
 * StringUtils.countMatches(null, *)       = 0
 * StringUtils.countMatches("", *)         = 0
 * StringUtils.countMatches("abba", 0)  = 0
 * StringUtils.countMatches("abba", 'a')   = 2
 * StringUtils.countMatches("abba", 'b')  = 2
 * StringUtils.countMatches("abba", 'x') = 0
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param ch  the char to count
 * @return the number of occurrences, 0 if the CharSequence is {@code null}
 * @since 3.4
 */
public static int countMatches(final CharSequence str, final char ch) {
    if (isEmpty(str)) {
        return 0;
    }
    int count = 0;
    // We could also call str.toCharArray() for faster look ups but that would generate more garbage.
    for (int i = 0; i < str.length(); i++) {
        if (ch == str.charAt(i)) {
            count++;
        }
    }
    return count;
}