List of usage examples for java.lang CharSequence charAt
char charAt(int index);
From source file:com.sjdf.platform.xss.StringUtils.java
/** * <p>//www . ja v a 2s . com * Search a CharSequence to find the first index of any character in the * given set of characters. * </p> * <p/> * <p> * A {@code null} String will return {@code -1}. A {@code null} or zero * length search array will return {@code -1}. * </p> * <p/> * <pre> * 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 3.0 Changed signature from indexOfAny(String, char[]) to * indexOfAny(CharSequence, char...) */ public static int indexOfAny(CharSequence cs, char... searchChars) { if (isEmpty(cs) || searchChars == null) { 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.sjdf.platform.xss.StringUtils.java
/** * <p>/*w w w . j a va 2 s. c om*/ * Searches a CharSequence to find the first index of any character not in * the given set of characters. * </p> * <p/> * <p> * A {@code null} CharSequence will return {@code -1}. A {@code null} or * zero length search array will return {@code -1}. * </p> * <p/> * <pre> * 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 3.0 Changed signature from indexOfAnyBut(String, char[]) to * indexOfAnyBut(CharSequence, char...) */ public static int indexOfAnyBut(CharSequence cs, char... searchChars) { if (isEmpty(cs) || searchChars == null) { 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.sjdf.platform.xss.StringUtils.java
/** * <p>// ww w . java2 s. c o m * Checks if the CharSequence contains any character in the given set of * characters. * </p> * <p/> * <p> * A {@code null} CharSequence will return {@code false}. A {@code null} or * zero length search array will return {@code false}. * </p> * <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 3.0 Changed signature from containsAny(String, char[]) to * containsAny(CharSequence, char...) */ public static boolean containsAny(CharSequence cs, char... searchChars) { if (isEmpty(cs) || searchChars == null) { 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:com.rdm.common.util.StringUtils.java
/** * Calculates the number of transposition between two strings. * @param first The first string./* ww w.j a v a2 s . c om*/ * @param second The second string. * @return The number of transposition between the two strings. */ private static int transpositions(CharSequence first, CharSequence second) { int transpositions = 0; for (int i = 0; i < first.length(); i++) { if (first.charAt(i) != second.charAt(i)) { transpositions++; } } return transpositions / 2; }
From source file:com.rdm.common.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>// www. j a v a2 s . co 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 * 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)) == false) { return false; } } return true; }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>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>/*from ww w . j a va 2 s .c o m*/ * StringUtils.getLevenshteinDistance(null, *) = IllegalArgumentException * StringUtils.getLevenshteinDistance(*, null) = IllegalArgumentException * StringUtils.getLevenshteinDistance("","") = 0 * StringUtils.getLevenshteinDistance("","a") = 1 * StringUtils.getLevenshteinDistance("aaapppp", "") = 7 * StringUtils.getLevenshteinDistance("frog", "fog") = 1 * StringUtils.getLevenshteinDistance("fly", "ant") = 3 * StringUtils.getLevenshteinDistance("elephant", "hippo") = 7 * StringUtils.getLevenshteinDistance("hippo", "elephant") = 7 * StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8 * StringUtils.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} * @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]; }
From source file:bfile.util.StringUtils.java
/** * <p>Checks if the CharSequence contains only Unicode digits or space * ({@code ' '})./*from w ww .ja va 2 s . com*/ * 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.isNumeric("\u0967\u0968\u0969") = true * StringUtils.isNumeric("\u0967\u0968 \u0969") = 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(final CharSequence cs) { if (cs == null) { return false; } final 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:bfile.util.StringUtils.java
/** * <p>Checks if the CharSequence contains only Unicode letters and * space (' ').</p>/*from www . j a v a 2 s.c o m*/ * * <p>{@code null} will return {@code false} * An empty CharSequence (length()=0) will return {@code true}.</p> * * <pre> * StringUtils.isAlphaSpace(null) = false * StringUtils.isAlphaSpace("") = true * StringUtils.isAlphaSpace(" ") = true * StringUtils.isAlphaSpace("abc") = true * StringUtils.isAlphaSpace("ab c") = true * StringUtils.isAlphaSpace("ab2c") = false * StringUtils.isAlphaSpace("ab-c") = false * </pre> * * @param cs the CharSequence to check, may be null * @return {@code true} if only contains letters and space, * and is non-null * @since 3.0 Changed signature from isAlphaSpace(String) to isAlphaSpace(CharSequence) */ public static boolean isAlphaSpace(final CharSequence cs) { if (cs == null) { return false; } final int sz = cs.length(); for (int i = 0; i < sz; i++) { if (Character.isLetter(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.</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 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(final CharSequence cs) { if (isEmpty(cs)) { return false; } final int sz = cs.length(); for (int i = 0; i < sz; i++) { if (Character.isLetter(cs.charAt(i)) == false) { return false; } } return true; }