List of usage examples for java.lang CharSequence length
int length();
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Check if a CharSequence ends with a specified suffix (optionally case insensitive).</p> * * @see java.lang.String#endsWith(String) * @param str the CharSequence to check, may be null * @param suffix the suffix to find, may be null * @param ignoreCase indicates whether the compare should ignore case * (case insensitive) or not.//from www .ja v a 2s.c om * @return {@code true} if the CharSequence starts with the prefix or * both {@code null} */ private static boolean endsWith(CharSequence str, CharSequence suffix, boolean ignoreCase) { if (str == null || suffix == null) { return str == null && suffix == null; } if (suffix.length() > str.length()) { return false; } int strOffset = str.length() - suffix.length(); return CharSequenceUtils.regionMatches(str, ignoreCase, strOffset, suffix, 0, suffix.length()); }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Checks that the CharSequence does not contain certain characters.</p> * * <p>A {@code null} CharSequence will return {@code true}. * A {@code null} invalid character array will return {@code true}. * An empty CharSequence (length()=0) always returns true.</p> * * <pre>// w w w .jav a 2 s .c om * StringUtils.containsNone(null, *) = true * StringUtils.containsNone(*, null) = true * StringUtils.containsNone("", *) = true * StringUtils.containsNone("ab", '') = true * StringUtils.containsNone("abab", 'xyz') = true * StringUtils.containsNone("ab1", 'xyz') = true * StringUtils.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:org.apache.commons.lang3.StringUtils.java
/** * <p>Finds the last index within a CharSequence, handling {@code null}. * This method uses {@link String#lastIndexOf(int)} if possible.</p> * * <p>A {@code null} or empty ("") CharSequence will return {@code -1}.</p> * * <pre>// w ww. j ava 2 s. c o m * StringUtils.lastIndexOf(null, *) = -1 * StringUtils.lastIndexOf("", *) = -1 * StringUtils.lastIndexOf("aabaabaa", 'a') = 7 * StringUtils.lastIndexOf("aabaabaa", 'b') = 5 * </pre> * * @param seq the CharSequence to check, may be null * @param searchChar the character to find * @return the last index of the search character, * -1 if no match or {@code null} string input * @since 2.0 * @since 3.0 Changed signature from lastIndexOf(String, int) to lastIndexOf(CharSequence, int) */ public static int lastIndexOf(CharSequence seq, int searchChar) { if (isEmpty(seq)) { return INDEX_NOT_FOUND; } return CharSequenceUtils.lastIndexOf(seq, searchChar, seq.length()); }
From source file:org.apache.commons.lang3.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>/* www .j a v a 2s .co 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(CharSequence seq, CharSequence searchChars) { if (isEmpty(seq) || isEmpty(searchChars)) { return INDEX_NOT_FOUND; } int strLen = seq.length(); for (int i = 0; i < strLen; i++) { char ch = seq.charAt(i); boolean chFound = CharSequenceUtils.indexOf(searchChars, ch, 0) >= 0; if (i + 1 < strLen && Character.isHighSurrogate(ch)) { 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:org.apache.commons.lang3.StringUtils.java
/** * <p>Checks if the CharSequence contains any character in the given * set of characters.</p>//from ww w .jav a 2 s . c om * * <p>A {@code null} CharSequence will return {@code false}. * A {@code null} or zero length search array will return {@code false}.</p> * * <pre> * StringUtils.containsAny(null, *) = false * StringUtils.containsAny("", *) = false * StringUtils.containsAny(*, null) = false * StringUtils.containsAny(*, []) = false * StringUtils.containsAny("zzabyycdxx",['z','a']) = true * StringUtils.containsAny("zzabyycdxx",['b','y']) = true * StringUtils.containsAny("aba", ['z']) = false * </pre> * * @param cs the CharSequence to check, may be null * @param searchChars the chars to search for, may be null * @return the {@code true} if any of the chars are found, * {@code false} if no match or null input * @since 2.4 * @since 3.0 Changed signature from containsAny(String, char[]) to containsAny(CharSequence, char...) */ public static boolean containsAny(CharSequence cs, char... searchChars) { if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) { return false; } int csLength = cs.length(); int searchLength = searchChars.length; int csLast = csLength - 1; int searchLast = searchLength - 1; for (int i = 0; i < csLength; i++) { char ch = cs.charAt(i); for (int j = 0; j < searchLength; j++) { if (searchChars[j] == ch) { if (Character.isHighSurrogate(ch)) { if (j == searchLast) { // missing low surrogate, fine, like String.indexOf(String) return true; } if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) { return true; } } else { // ch is in the Basic Multilingual Plane return true; } } } } return false; }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Search a CharSequence to find the first index of any * character in the given set of characters.</p> * * <p>A {@code null} String will return {@code -1}. * A {@code null} or zero length search array will return {@code -1}.</p> * * <pre>//from w ww . j a va 2 s .c o m * StringUtils.indexOfAny(null, *) = -1 * StringUtils.indexOfAny("", *) = -1 * StringUtils.indexOfAny(*, null) = -1 * StringUtils.indexOfAny(*, []) = -1 * StringUtils.indexOfAny("zzabyycdxx",['z','a']) = 0 * StringUtils.indexOfAny("zzabyycdxx",['b','y']) = 3 * StringUtils.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) || ArrayUtils.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:org.apache.commons.lang3.StringUtils.java
/** * <p>Searches 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 zero length search array will return {@code -1}.</p> * * <pre>/*from www . j a v a2s .co m*/ * StringUtils.indexOfAnyBut(null, *) = -1 * StringUtils.indexOfAnyBut("", *) = -1 * StringUtils.indexOfAnyBut(*, null) = -1 * StringUtils.indexOfAnyBut(*, []) = -1 * StringUtils.indexOfAnyBut("zzabyycdxx", new char[] {'z', 'a'} ) = 3 * StringUtils.indexOfAnyBut("aba", new char[] {'z'} ) = 0 * StringUtils.indexOfAnyBut("aba", new char[] {'a', 'b'} ) = -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) || ArrayUtils.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; }