List of usage examples for java.lang CharSequence charAt
char charAt(int index);
From source file:org.getlantern.firetweet.activity.support.UserProfileEditorActivity.java
private static boolean stringEquals(final CharSequence str1, final CharSequence str2) { if (str1 == null || str2 == null) return str1 == str2; if (str1.length() != str2.length()) return false; for (int i = 0, j = str1.length(); i < j; i++) { if (str1.charAt(i) != str2.charAt(i)) return false; }/*w w w. j av a2 s . com*/ return true; }
From source file:eap.util.EDcodeUtil.java
public static byte[] hexDecode(CharSequence s) { int nChars = s.length(); if (nChars % 2 != 0) { throw new IllegalArgumentException("Hex-encoded string must have an even number of characters"); }/*from ww w . jav a 2s . co m*/ byte[] result = new byte[nChars / 2]; for (int i = 0; i < nChars; i += 2) { int msb = Character.digit(s.charAt(i), 16); int lsb = Character.digit(s.charAt(i + 1), 16); if (msb < 0 || lsb < 0) { throw new IllegalArgumentException("Non-hex character in input: " + s); } result[i / 2] = (byte) ((msb << 4) | lsb); } return result; }
From source file:com.liferay.events.global.mobile.Utils.java
/** * Calculates the number of transposition between two strings. * * @param first The first string.// w w w.j a va 2 s . c om * @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./*from w w w .j a va2s. c o m*/ * * @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:com.webbfontaine.valuewebb.model.util.Utils.java
public static String toWords(CharSequence str) { StringBuilder res = new StringBuilder(str.length()); for (int i = 0; i < str.length(); i++) { Character ch = str.charAt(i); if (Character.isUpperCase(ch)) { res.append(' ').append(ch); } else {//from w ww . jav a 2s . co m res.append(ch); } } char c = Character.toUpperCase(res.charAt(0)); res.replace(0, 1, new String(new char[] { c })); return res.toString(); }
From source file:be.brunoparmentier.wifikeyshare.ui.activities.WifiNetworkActivity.java
private static void setPasswordRestrictions(EditText editText) { // Source: http://stackoverflow.com/a/4401227 InputFilter filter = new InputFilter() { @Override/*from w ww.j ava 2 s .c om*/ public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { // TODO: check that the filter follows WEP/WPA recommendations for (int i = start; i < end; i++) { if (!Character.isLetterOrDigit(source.charAt(i))) { return ""; } } return null; } }; editText.setFilters(new InputFilter[] { filter }); }
From source file:org.renjin.parser.NumericLiterals.java
public static Complex parseComplex(CharSequence s) { int lastCharIndex = s.length() - 1; if (s.charAt(lastCharIndex) == 'i') { // parse as number with imaginary component int imaginaryStart = findImaginaryStart(s); if (imaginaryStart <= 0) { // malformed return ComplexVector.NA; }//from ww w .jav a 2s . co m double real = parseDouble(s, 0, imaginaryStart, '.', true); if (DoubleVector.isNA(real)) { return ComplexVector.NA; } double imaginary = parseDouble(s, imaginaryStart, lastCharIndex, '.', true); return ComplexVector.complex(real, imaginary); } else { // parse as number with only real component double real = parseDouble(s, 0, s.length(), '.', true); return ComplexVector.complex(real); } }
From source file:HSqlPrimerDesign.java
public static double gcContent(CharSequence primer) { int i = 0;/* ww w.ja va 2 s .c om*/ 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:httpmultiplexer.httpproxy.ProxyServlet.java
/** * Encodes characters in the query or fragment part of the URI. * * <p>/*from w w w. j ava2s . c o m*/ * 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:org.codehaus.groovy.grails.web.pages.Parse.java
private static boolean match(CharSequence pat, CharSequence text, int start) { int ix = start, ixz = text.length(), ixy = start + pat.length(); if (ixz > ixy) ixz = ixy;//w w w . j a v a 2s. c om if (pat.length() > ixz - start) return false; for (; ix < ixz; ix++) { if (Character.toLowerCase(text.charAt(ix)) != Character.toLowerCase(pat.charAt(ix - start))) { return false; } } return true; }