List of usage examples for java.lang CharSequence charAt
char charAt(int index);
From source file:Strings.java
/** * Returns {@code true} if the specified character sequence is a * valid sequence of UTF-16 {@code char} values. A sequence is * legal if each high surrogate {@code char} value is followed by * a low surrogate value (as defined by {@link * Character#isHighSurrogate(char)} and {@link * Character#isLowSurrogate(char)})./*from ww w . j a v a 2 s. c om*/ * * <p>This method does <b>not</b> check to see if the sequence of * code points defined by the UTF-16 consists only of code points * defined in the latest Unicode standard. The method only tests * the validity of the UTF-16 encoding sequence. * * @param cs Character sequence to test. * @return {@code true} if the sequence of characters is * legal in UTF-16. */ public static boolean isLegalUtf16(CharSequence cs) { for (int i = 0; i < cs.length(); ++i) { char high = cs.charAt(i); if (Character.isLowSurrogate(high)) return false; if (!Character.isHighSurrogate(high)) continue; ++i; if (i >= cs.length()) return false; char low = cs.charAt(i); if (!Character.isLowSurrogate(low)) return false; int codePoint = Character.toCodePoint(high, low); if (!Character.isValidCodePoint(codePoint)) return false; } return true; }
From source file:Main.java
/** * Formats by adding a hyphen for every 4 numbers (IE like a credit card) * @param s Charsequence being altered./*from w w w.java2 s .c o m*/ * @return Return an altered String with hyphens in it */ public static String formatNumbersAsCreditCard(CharSequence s) { int groupDigits = 0; String tmp = ""; for (int i = 0; i < s.length(); ++i) { tmp += s.charAt(i); ++groupDigits; if (groupDigits == 4) { if (groupDigits == 16) { } else { tmp += "-"; } groupDigits = 0; } } if (tmp.length() == 20) { tmp = tmp.substring(0, tmp.length() - 1); //Get rid of last digit } return tmp; }
From source file:Main.java
public static int nmeaChecksum(CharSequence nmea, int startIndex, int endIndex) { int checkSum = 0; for (int c = startIndex; c < endIndex; c++) { checkSum ^= (int) nmea.charAt(c); }/*from w w w. j a va 2 s. c om*/ return checkSum; }
From source file:Main.java
public static boolean validUTF16String(CharSequence s) { final int size = s.length(); for (int i = 0; i < size; i++) { char ch = s.charAt(i); if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { if (i < size - 1) { i++;//from w w w . j a v a 2 s . c o m char nextCH = s.charAt(i); if (nextCH >= UNI_SUR_LOW_START && nextCH <= UNI_SUR_LOW_END) { // Valid surrogate pair } else // Unmatched high surrogate return false; } else // Unmatched high surrogate return false; } else if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) // Unmatched low surrogate return false; } return true; }
From source file:Main.java
/** * @return true if substring started at 'start' is a XML entity reference. */// ww w . ja v a2s .co m public static boolean isXmlEntityRef(CharSequence str, int start) { int len = str.length(); if (start >= len || str.charAt(start) != '&') return false; if (++start >= len) return false; if (str.charAt(start) == '#') return isXmlCharRefPart(str, start + 1); int i = start; if (!isNameStart(str.charAt(i))) return false; for (++i; i < len; ++i) { if (!isName(str.charAt(i))) break; } return (i > start && i < len && str.charAt(i) == ';'); }
From source file:Main.java
/** * Clean strings from illegal XML 1.0 characters. * See <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets">XML charset</a> * * @param string string to clean//from www . ja v a2 s .c o m * @return the cleaned string */ public static String sanitize(CharSequence string) { StringBuilder sb = new StringBuilder(); for (int i = 0, len = string.length(); i < len; i++) { char c = string.charAt(i); boolean legal = c == '\u0009' || c == '\n' || c == '\r' || (c >= '\u0020' && c <= '\uD7FF') || (c >= '\uE000' && c <= '\uFFFD'); if (legal) { sb.append(c); } } return sb.toString(); }
From source file:Main.java
/** * utility to get the part of a charsequence that is not the NCName * fragment./*from w w w . j ava 2s. c o m*/ * * @param s * the charsequence to split * @return the prefix split at the last non-ncname character, or the whole * input if no ncname is found */ public static String getNCNamePrefix(CharSequence s) { if (s.length() > 1 && s.charAt(0) == '_' && s.charAt(1) == ':') { return s.toString(); } int localPartStartIndex = getNCNameSuffixIndex(s); if (localPartStartIndex > -1) { return s.toString().substring(0, localPartStartIndex); } else { return s.toString(); } }
From source file:Main.java
/** * Does one string contain another, starting at a specific offset? * @param text/* w ww . j av a2s . c o m*/ * @param offset * @param other * @return */ public static int matchesAt(CharSequence text, int offset, CharSequence other) { int len = other.length(); int i = 0; int j = offset; for (; i < len; ++i, ++j) { char pc = other.charAt(i); char tc = text.charAt(j); if (pc != tc) return -1; } return i; }
From source file:Main.java
/** * Finds the length of the largest prefix for two character sequences. * * @param a character sequence// www . j ava 2 s .c o m * @param b character sequence * @return the length of largest prefix of <code>a</code> and <code>b</code> * @throws IllegalArgumentException if either <code>a</code> or <code>b</code> * is <code>null</code> */ public static int largestPrefixLength(CharSequence a, CharSequence b) { int len = 0; for (int i = 0; i < Math.min(a.length(), b.length()); ++i) { if (a.charAt(i) != b.charAt(i)) break; ++len; } return len; }
From source file:Main.java
public static void escape(StringBuilder sb, CharSequence string) { for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); switch (c) { case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '"': sb.append("""); break; default:/*from ww w . ja v a 2 s . c om*/ sb.append(c); } } }