List of usage examples for java.lang CharSequence length
int length();
From source file:org.cleverbus.common.Strings.java
/** * This code was copied from Apache Wicket. * Converts a Java String to an HTML markup String by replacing illegal characters with HTML * entities where appropriate. Spaces are converted to non-breaking spaces (<nbsp>) if * escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are * converted to &lt; entities and greater than signs to &gt; entities. * * @param s the string to escape//from ww w. ja v a2s.c o m * @param escapeSpaces true to replace ' ' with nonbreaking space * @param convertToHtmlUnicodeEscapes true to convert non-7 bit characters to unicode HTML (&#...) * @return the escaped string */ public static CharSequence escapeMarkup(CharSequence s, boolean escapeSpaces, boolean convertToHtmlUnicodeEscapes) { if (s == null) { return null; } else { int len = s.length(); final StringBuilder sb = new StringBuilder((int) (len * 1.1)); for (int i = 0; i < len; i++) { final char c = s.charAt(i); switch (c) { case '\t': if (escapeSpaces) { // Assumption is four space tabs (sorry, but that's // just how it is!) sb.append(" "); } else { sb.append(c); } break; case ' ': if (escapeSpaces) { sb.append(" "); } else { sb.append(c); } break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append(AMP_XML); break; case '"': sb.append("""); break; case '\'': sb.append("'"); break; default: int ci = 0xffff & c; if (ci < 0x20) { // http://en.wikipedia.org/wiki/Valid_characters_in_XML if ((ci != 0x09) && (ci != 0x0A) && (ci != 0x0D)) { sb.append("&#"); sb.append(Integer.toString(ci)); sb.append(';'); break; } } if (convertToHtmlUnicodeEscapes) { if (ci < 160) { // nothing special only 7 Bit sb.append(c); } else { // Not 7 Bit use the unicode system sb.append("&#"); sb.append(Integer.toString(ci)); sb.append(';'); } } else { sb.append(c); } break; } } return sb; } }
From source file:edu.cornell.med.icb.goby.modes.FastaToCompactMode.java
public static byte[] convertQualityScores(final QualityEncoding qualityEncoding, final CharSequence quality, final boolean verboseQualityScores, final boolean apiMode) { // Only Solexa, Sanger and Illumina encoding are supported at this time final int size = quality.length(); final byte[] qualityScoreBuffer = new byte[size]; if (verboseQualityScores) { System.out.println(quality); }//from w w w.j a v a 2 s. c o m for (int position = 0; position < size; position++) { qualityScoreBuffer[position] = qualityEncoding .asciiEncodingToPhredQualityScore(quality.charAt(position)); if (!qualityEncoding.isWithinValidRange(qualityScoreBuffer[position])) { final String message = "Phred quality scores must be within specific ranges for specfic encodings. " + "The value decoded was " + qualityScoreBuffer[position] + " and outside of the valid range for " + qualityEncoding + " You may have selected an incorrect encoding."; if (apiMode) { throw new IllegalArgumentException(message); } else { System.err.println(message); System.exit(10); } } if (verboseQualityScores) { System.out.print(qualityScoreBuffer[position]); System.out.print(" "); } } if (verboseQualityScores) { System.out.println(); } return qualityScoreBuffer; }
From source file:com.liferay.events.global.mobile.Utils.java
/** * Calculates the number of transposition between two strings. * * @param first The first string./*from w w w .j a v a2 s. c o m*/ * @param second The second string. * @return The number of transposition between the two strings. */ private static int transpositions(final CharSequence first, final 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:org.owasp.appsensor.block.proxy.servlet.ProxyServlet.java
/** * Encodes characters in the query or fragment part of the URI. * * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. * To be more forgiving, we must escape the problematic characters. See the URI class for the * spec.//ww w . j a va2s . c om * * @param in example: name=value&foo=bar#fragment */ @SuppressWarnings("resource") protected static CharSequence encodeUriQuery(CharSequence in) { //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things. StringBuilder outBuf = null; Formatter formatter = null; for (int i = 0; i < in.length(); i++) { char c = in.charAt(i); boolean escape = true; if (c < 128) { if (asciiQueryChars.get((int) c)) { escape = false; } } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii escape = false; } if (!escape) { if (outBuf != null) outBuf.append(c); } else { //escape if (outBuf == null) { outBuf = new StringBuilder(in.length() + 5 * 3); outBuf.append(in, 0, i); formatter = new Formatter(outBuf); } //leading %, 0 padded, width 2, capital hex formatter.format("%%%02X", (int) c);//TODO } } return outBuf != null ? outBuf : in; }
From source file:HSqlPrimerDesign.java
public static double gcContent(CharSequence primer) { int i = 0;// w w w. j ava2s . co m int count = 0; while (i < primer.length()) { char nuc = primer.charAt(i); if ((nuc == 'G') || (nuc == 'C') || (nuc == 'g') || (nuc == 'c')) { count += 1; } i++; } return count * 1.0 / primer.length(); }
From source file:com.aiblockchain.api.StringUtils.java
/** * Returns whether the given CharSequence is neither <code>null</code> nor length 0. Note: Will return <code>true</code> for a * CharSequence that purely consists of whitespace. * * @param str the CharSequence to check (may be <code>null</code>) * * @return <code>true</code> if the CharSequence is not null and has length * @see #hasText(String)/*from w w w . j av a 2 s . c o m*/ */ public static boolean hasLength(final CharSequence str) { return (str != null && str.length() > 0); }
From source file:Main.java
public static void setTitle(TextView titleTv, int titleColor, int titleTextSize, int messageColor, int messageTextSize, CharSequence title, CharSequence message) { titleTv.setMinHeight(titleTextSize * 3); if (!TextUtils.isEmpty(title) && TextUtils.isEmpty(message)) { titleTv.setTextColor(titleColor); titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize); titleTv.getPaint().setFakeBoldText(true); titleTv.setText(title);//w w w. j av a2 s.co m } else if (TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) { titleTv.setTextColor(messageColor); titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, messageTextSize); titleTv.setText(message); } else if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) { SpannableString titleSs = new SpannableString(title + "\n" + message); titleSs.setSpan(new ForegroundColorSpan(titleColor), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new AbsoluteSizeSpan(titleTextSize), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new ForegroundColorSpan(messageColor), title.length(), titleSs.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new AbsoluteSizeSpan(messageTextSize), title.length(), titleSs.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleTv.setText(titleSs); titleTv.setLineSpacing(0.0f, 1.2f); } else { titleTv.setVisibility(View.GONE); } }
From source file:httpmultiplexer.httpproxy.ProxyServlet.java
/** * Encodes characters in the query or fragment part of the URI. * * <p>//from www.j a v a 2 s .c om * Unfortunately, an incoming URI sometimes has characters disallowed by the * spec. HttpClient insists that the outgoing proxied request has a valid * URI because it uses Java's {@link URI}. To be more forgiving, we must * escape the problematic characters. See the URI class for the spec. * * @param in example: name=value&foo=bar#fragment */ protected static CharSequence encodeUriQuery(CharSequence in) { //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things. StringBuilder outBuf = null; Formatter formatter = null; for (int i = 0; i < in.length(); i++) { char c = in.charAt(i); boolean escape = true; if (c < 128) { if (asciiQueryChars.get((int) c)) { escape = false; } } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii escape = false; } if (!escape) { if (outBuf != null) { outBuf.append(c); } } else { //escape if (outBuf == null) { outBuf = new StringBuilder(in.length() + 5 * 3); outBuf.append(in, 0, i); formatter = new Formatter(outBuf); } //leading %, 0 padded, width 2, capital hex formatter.format("%%%02X", (int) c);//TODO } } return outBuf != null ? outBuf : in; }
From source file:com.oncore.calorders.core.utils.FormatHelper.java
/** * The <code>isNameValid</code> method checks if the CharSequence contains * only unicode letters, spaces, apostrophes, and reverse apostrophes * * <pre>// w ww .ja va 2 s . c o m * PageUtilities.isNameValid(null) = false * PageUtilities.isNameValid("") = false * PageUtilities.isNameValid(" ") = false * PageUtilities.isNameValid("abc") = true * PageUtilities.isNameValid("ab c") = true * PageUtilities.isNameValid("ab2c") = false * PageUtilities.isNameValid("ab-c") = false * PageUtilities.isNameValid("9999") = false * PageUtilities.isNameValid("For instance, this is bla") = false * PageUtilities.isNameValid("John Doe") = true * PageUtilities.isNameValid("John O'Brien") = true * PageUtilities.isNameValid("John O`Brien") = true * PageUtilities.isNameValid("John") = true * PageUtilities.isNameValid("Smith") = true * PageUtilities.isNameValid("John O@Brien") = false * </pre> * * @author OnCore Consulting LLC * * @param cs the CharSequence to check, may be null * * @return true if only unicode letters, spaces, apostrophes, and reverse * apostrophes, false otherwise */ @SuppressWarnings("empty-statement") public static Boolean isNameValid(final CharSequence cs) { if (StringUtils.isBlank(String.valueOf(cs))) { return Boolean.FALSE; } final int size = cs.length(); for (int i = 0; i < size; i++) { if ('\'' == cs.charAt(i) || '`' == cs.charAt(i) || cs.charAt(i) == ' ' || (Character.isLetter(cs.charAt(i)) == true)) { ; // do nothing } else { return Boolean.FALSE; } } return Boolean.TRUE; }
From source file:com.liferay.events.global.mobile.Utils.java
/** * This method returns the Jaro-Winkler score for string matching. * * @param first the first string to be matched * @param second the second string to be machted * @return matching score without scaling factor impact *//*from ww w .ja va 2 s . c o m*/ private static double score(final CharSequence first, final CharSequence second) { String shorter; String longer; // Determine which String is longer. if (first.length() > second.length()) { longer = first.toString().toLowerCase(); shorter = second.toString().toLowerCase(); } else { longer = second.toString().toLowerCase(); shorter = first.toString().toLowerCase(); } // Calculate the half length() distance of the shorter String. final int halflength = shorter.length() / 2 + 1; // Find the set of matching characters between the shorter and longer strings. Note that // the set of matching characters may be different depending on the order of the strings. final String m1 = getSetOfMatchingCharacterWithin(shorter, longer, halflength); final String m2 = getSetOfMatchingCharacterWithin(longer, shorter, halflength); // If one or both of the sets of common characters is empty, then // there is no similarity between the two strings. if (m1.length() == 0 || m2.length() == 0) { return 0.0; } // If the set of common characters is not the same size, then // there is no similarity between the two strings, either. if (m1.length() != m2.length()) { return 0.0; } // Calculate the number of transposition between the two sets // of common characters. final int transpositions = transpositions(m1, m2); // Calculate the distance. return (m1.length() / ((double) shorter.length()) + m2.length() / ((double) longer.length()) + (m1.length() - transpositions) / ((double) m1.length())) / 3.0; }