Example usage for java.lang CharSequence length

List of usage examples for java.lang CharSequence length

Introduction

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

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode letters, digits
 * or space ({@code ' '}).</p>//from   www.  j a v a 2s  .co m
 *
 * <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(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) && (cs.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode digits or space
 * ({@code ' '}).//from  w w  w  . j ava2 s .co  m
 * 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 true}.</p>
 *
 * <pre>
 * StringUtils.isNumericSpace(null)   = false
 * StringUtils.isNumericSpace("")     = true
 * StringUtils.isNumericSpace("  ")   = true
 * StringUtils.isNumericSpace("123")  = true
 * StringUtils.isNumericSpace("12 3") = true
 * StringUtils.isNumericSpace("ab2c") = false
 * StringUtils.isNumericSpace("12-3") = false
 * StringUtils.isNumericSpace("12.3") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains digits or space,
 *  and is non-null
 * @since 3.0 Changed signature from isNumericSpace(String) to isNumericSpace(CharSequence)
 */
public static boolean isNumericSpace(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isDigit(cs.charAt(i)) == false) && (cs.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}

From source file:ths.commons.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>//from  www . j  av  a  2s .c om
 * 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(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    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:ths.commons.util.StringUtils.java

/**
 * <p>Check if a CharSequence starts with a specified prefix (optionally case insensitive).</p>
 *
 * @see java.lang.String#startsWith(String)
 * @param str  the CharSequence to check, may be null
 * @param prefix the prefix to find, may be null
 * @param ignoreCase indicates whether the compare should ignore case
 *  (case insensitive) or not./*from  w w w  .ja  v a 2  s.  c o  m*/
 * @return {@code true} if the CharSequence starts with the prefix or
 *  both {@code null}
 */
private static boolean startsWith(CharSequence str, CharSequence prefix, boolean ignoreCase) {
    if (str == null || prefix == null) {
        return (str == null && prefix == null);
    }
    if (prefix.length() > str.length()) {
        return false;
    }
    return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, prefix.length());
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Case in-sensitive find of the last index within a CharSequence
 * from the specified position.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A negative start position returns {@code -1}.
 * An empty ("") search CharSequence always matches unless the start position is negative.
 * A start position greater than the string length searches the whole string.</p>
 *
 * <pre>/*from w w w . ja  v  a 2s.  c om*/
 * StringUtils.lastIndexOfIgnoreCase(null, *, *)          = -1
 * StringUtils.lastIndexOfIgnoreCase(*, null, *)          = -1
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8)  = 7
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8)  = 5
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9)  = 5
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0)  = -1
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStr  the CharSequence to find, may be null
 * @param startPos  the start position
 * @return the first index of the search CharSequence,
 *  -1 if no match or {@code null} input
 * @since 2.5
 * @since 3.0 Changed signature from lastIndexOfIgnoreCase(String, String, int) to lastIndexOfIgnoreCase(CharSequence, CharSequence, int)
 */
public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return INDEX_NOT_FOUND;
    }
    if (startPos > (str.length() - searchStr.length())) {
        startPos = str.length() - searchStr.length();
    }
    if (startPos < 0) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }

    for (int i = startPos; i >= 0; i--) {
        if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * Gets a CharSequence length or {@code 0} if the CharSequence is
 * {@code null}.//from   w  w w .  j  av a2s. c o  m
 *
 * @param cs
 *            a CharSequence or {@code null}
 * @return CharSequence length or {@code 0} if the CharSequence is
 *         {@code null}.
 * @since 2.4
 * @since 3.0 Changed signature from length(String) to length(CharSequence)
 */
public static int length(CharSequence cs) {
    return cs == null ? 0 : cs.length();
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only Unicode letters.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code false}.</p>
 *
 * <pre>//from ww w. ja  v a2  s .c  om
 * StringUtils.isAlpha(null)   = false
 * StringUtils.isAlpha("")     = false
 * StringUtils.isAlpha("  ")   = false
 * StringUtils.isAlpha("abc")  = true
 * StringUtils.isAlpha("ab2c") = false
 * StringUtils.isAlpha("ab-c") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if only contains letters, and is non-null
 * @since 3.0 Changed signature from isAlpha(String) to isAlpha(CharSequence)
 * @since 3.0 Changed "" to return false and not true
 */
public static boolean isAlpha(CharSequence cs) {
    if (cs == null || cs.length() == 0) {
        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:org.apache.commons.lang3.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   w w w  . j  a  v a  2s  .  c  o m
 * 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(CharSequence cs) {
    if (cs == null || cs.length() == 0) {
        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:org.apache.commons.lang3.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>
 *
 * <pre>/*from w  ww .  j a v a2s .  c  o  m*/
 * StringUtils.isNumeric(null)   = false
 * StringUtils.isNumeric("")     = false
 * StringUtils.isNumeric("  ")   = false
 * StringUtils.isNumeric("123")  = true
 * StringUtils.isNumeric("12 3") = false
 * StringUtils.isNumeric("ab2c") = false
 * StringUtils.isNumeric("12-3") = false
 * StringUtils.isNumeric("12.3") = 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(CharSequence cs) {
    if (cs == null || cs.length() == 0) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isDigit(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:ths.commons.util.StringUtils.java

/**
 * <p>Case in-sensitive find of the first index within a CharSequence
 * from the specified position.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A negative start position is treated as zero.
 * An empty ("") search CharSequence always matches.
 * A start position greater than the string length only matches
 * an empty search CharSequence.</p>
 *
 * <pre>//from ww w .j a va2  s .c  om
 * StringUtils.indexOfIgnoreCase(null, *, *)          = -1
 * StringUtils.indexOfIgnoreCase(*, null, *)          = -1
 * StringUtils.indexOfIgnoreCase("", "", 0)           = 0
 * StringUtils.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
 * StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
 * StringUtils.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
 * StringUtils.indexOfIgnoreCase("abc", "", 9)        = 3
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStr  the CharSequence to find, may be null
 * @param startPos  the start position, negative treated as zero
 * @return the first index of the search CharSequence,
 *  -1 if no match or {@code null} string input
 * @since 2.5
 * @since 3.0 Changed signature from indexOfIgnoreCase(String, String, int) to indexOfIgnoreCase(CharSequence, CharSequence, int)
 */
public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return INDEX_NOT_FOUND;
    }
    if (startPos < 0) {
        startPos = 0;
    }
    int endLimit = (str.length() - searchStr.length()) + 1;
    if (startPos > endLimit) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }
    for (int i = startPos; i < endLimit; i++) {
        if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return INDEX_NOT_FOUND;
}