List of usage examples for java.lang CharSequence length
int length();
From source file:Main.java
/** * Test if a format string contains the given designator. Always returns * {@code false} if the input format is {@code null}. * * @hide// w w w. j a v a 2 s .c om */ public static boolean hasDesignator(CharSequence inFormat, char designator) { if (inFormat == null) return false; final int length = inFormat.length(); int c; int count; for (int i = 0; i < length; i += count) { count = 1; c = inFormat.charAt(i); if (c == QUOTE) { count = skipQuotedText(inFormat, i, length); } else if (c == designator) { return true; } } return false; }
From source file:Main.java
/** * Returns true if a and b are equal, including if they are both null. * <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if * both the arguments were instances of String.</i></p> * * @param a first CharSequence to check//from w ww.j a va 2 s . c o m * @param b second CharSequence to check * * @return true if a and b are equal * * NOTE: Logic slightly change due to strict policy on CI - * "Inner assignments should be avoided" */ static boolean equals(CharSequence a, CharSequence b) { if (a == b) return true; if (a != null && b != null) { int length = a.length(); if (length == b.length()) { if (a instanceof String && b instanceof String) { return a.equals(b); } else { for (int i = 0; i < length; i++) { if (a.charAt(i) != b.charAt(i)) return false; } return true; } } } return false; }
From source file:Main.java
public static void escAttrValue(StringBuilder ret, CharSequence value, char delim) { for (int i = 0, len = value.length(); i < len; i++) { char c = value.charAt(i); switch (c) { case '&': if (isXmlEntityRef(value, i)) ret.append(c);//from w w w . jav a 2s. c o m else ret.append("&"); break; case '<': ret.append("<"); break; case '\'': if (c == delim) ret.append("'"); else ret.append(c); break; case '"': if (c == delim) ret.append("""); else ret.append(c); break; default: ret.append(c); } } }
From source file:net.gkovalechyn.minereset.Util.java
public static int LevenshteinDistance(CharSequence lhs, CharSequence rhs) { int len0 = lhs.length() + 1; int len1 = rhs.length() + 1; // the array of distances int[] cost = new int[len0]; int[] newcost = new int[len0]; // initial cost of skipping prefix in String s0 for (int i = 0; i < len0; i++) { cost[i] = i;//from w w w. j a va 2 s .c om } // dynamically computing the array of distances // transformation cost for each letter in s1 for (int j = 1; j < len1; j++) { // initial cost of skipping prefix in String s1 newcost[0] = j; // transformation cost for each letter in s0 for (int i = 1; i < len0; i++) { // matching current letters in both strings int match = (lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1; // computing cost for each transformation int cost_replace = cost[i - 1] + match; int cost_insert = cost[i] + 1; int cost_delete = newcost[i - 1] + 1; // keep minimum cost newcost[i] = Math.min(Math.min(cost_insert, cost_delete), cost_replace); } // swap cost/newcost arrays int[] swap = cost; cost = newcost; newcost = swap; } // the distance is the cost for transforming all letters in both strings return cost[len0 - 1]; }
From source file:Strings.java
/** * Returns a hash code for a character sequence that is equivalent * to the hash code generated for a its string yield. Recall that * the interface {@link CharSequence} does not refine the definition * of equality beyond that of {@link Object#equals(Object)}. * * <P>The return result is the same as would be produced by: * * <pre>/* w w w . j a v a 2 s . co m*/ * hashCode(cSeq) = cSeq.toString().hashCode()</pre> * * Recall that the {@link CharSequence} interface requires its * {@link CharSequence#toString()} to return a string * corresponding to its characters as returned by * <code>charAt(0),...,charAt(length()-1)</code>. This value * can be defined directly by inspecting the hash code for strings: * * <pre> * int h = 0; * for (int i = 0; i < cSeq.length(); ++i) * h = 31*h + cSeq.charAt(i); * return h;</pre> * * @param cSeq The character sequence. * @return The hash code for the specified character sequence. */ public static int hashCode(CharSequence cSeq) { if (cSeq instanceof String) return cSeq.hashCode(); int h = 0; for (int i = 0; i < cSeq.length(); ++i) h = 31 * h + cSeq.charAt(i); return h; }
From source file:TextUtils.java
/** * Tests if s starts with t, ignoring the case of the characters * //from w ww . j av a2s. com * @param s * @param t * @return <code>true</code> if s.toLowerCase().equals( t.toLowerCase() ), * but more efficiently */ public static boolean startsWithIgnoreCase(CharSequence s, CharSequence t) { if (s.length() < t.length()) { return false; } for (int i = 0; i < t.length(); i++) { char slc = Character.toLowerCase(s.charAt(i)); char tlc = Character.toLowerCase(t.charAt(i)); if (slc != tlc) { return false; } } return true; }
From source file:Main.java
public static CharSequence toLowerCase(CharSequence character) { if (character == null || character.equals("")) { return character; }// w w w . j a v a2 s . co m StringBuffer buffer = new StringBuffer(); for (int i = 0; i < character.length(); i++) { buffer.append(Character.toLowerCase(character.charAt(i))); } return buffer.toString(); }
From source file:Main.java
public static String escapeXml(CharSequence str) { if (str == null) { return null; }//from w ww .j a va2 s . c o m StringBuilder res = null; int strLength = str.length(); for (int i = 0; i < strLength; i++) { char c = str.charAt(i); String repl = encodeXMLChar(c); if (repl == null) { if (res != null) { res.append(c); } } else { if (res == null) { res = new StringBuilder(str.length() + 5); for (int k = 0; k < i; k++) { res.append(str.charAt(k)); } } res.append(repl); } } return res == null ? str.toString() : res.toString(); }
From source file:Main.java
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0; }/*from w ww. j a v a2 s. c o m*/ final int length = text.length(); final float[] widths = new float[length]; final int count; final Typeface savedTypeface = paint.getTypeface(); try { paint.setTypeface(getTextTypeface(text)); count = paint.getTextWidths(text, 0, length, widths); } finally { paint.setTypeface(savedTypeface); } int width = 0; for (int i = 0; i < count; i++) { width += Math.round(widths[i] + 0.5f); } return width; }
From source file:Main.java
/** * Gets the index of the longest NCName that is the suffix of a character * sequence.//w ww . j a v a2 s. c o m * * @param s * The character sequence. * @return The index of the longest suffix of the specified character * sequence {@code s} that is an NCName, or -1 if the character * sequence {@code s} does not have a suffix that is an NCName. */ public static int getNCNameSuffixIndex(CharSequence s) { // identify bnode labels and do not try to split them if (s.length() > 1 && s.charAt(0) == '_' && s.charAt(1) == ':') { return -1; } int index = -1; for (int i = s.length() - 1; i > -1; i--) { if (!Character.isLowSurrogate(s.charAt(i))) { int codePoint = Character.codePointAt(s, i); if (isNCNameStartChar(codePoint)) { index = i; } if (!isNCNameChar(codePoint)) { break; } } } return index; }