List of usage examples for java.lang String charAt
public char charAt(int index)
From source file:Main.java
/** * Checking whether a peer ID is Shadow style or not is a bit tricky. * /*from w w w .j a v a 2 s . c o m*/ * The BitTornado peer ID convention code is explained here: * http://forums.degreez.net/viewtopic.php?t=7070 * * The main thing we are interested in is the first six characters. * Although the other characters are base64 characters, there's no * guarantee that other clients which follow that style will follow * that convention (though the fact that some of these clients use * BitTornado in the core does blur the lines a bit between what is * "style" and what is just common across clients). * * So if we base it on the version number information, there's another * problem - there isn't the use of absolute delimiters (no fixed dash * character, for example). * * There are various things we can do to determine how likely the peer * ID is to be of that style, but for now, I'll keep it to a relatively * simple check. * * We'll assume that no client uses the fifth version digit, so we'll * expect a dash. We'll also assume that no client has reached version 10 * yet, so we expect the first two characters to be "letter,digit". * * We've seen some clients which don't appear to contain any version * information, so we need to allow for that. */ public static boolean isShadowStyle(String peer_id) { if (peer_id.charAt(5) != '-') { return false; } if (!Character.isLetter(peer_id.charAt(0))) { return false; } if (!(Character.isDigit(peer_id.charAt(1)) || peer_id.charAt(1) == '-')) { return false; } // Find where the version number string ends. int last_ver_num_index = 4; for (; last_ver_num_index > 0; last_ver_num_index--) { if (peer_id.charAt(last_ver_num_index) != '-') { break; } } // For each digit in the version string, check it is a valid version identifier. char c; for (int i = 1; i <= last_ver_num_index; i++) { c = peer_id.charAt(i); if (c == '-') { return false; } if (decodeAlphaNumericChar(c) == null) { return false; } } return true; }
From source file:Main.java
public static int binaryToAlgorism(String binary) { int max = binary.length(); int result = 0; for (int i = max; i > 0; i--) { char c = binary.charAt(i - 1); int algorism = c - '0'; result += Math.pow(2, max - i) * algorism; }/*w w w . ja v a 2s . c om*/ return result; }
From source file:Main.java
public static byte[] decodeBytes(String str) { byte[] bytes = new byte[str.length() / 2]; for (int i = 0; i < str.length(); i += 2) { char c = str.charAt(i); bytes[i / 2] = (byte) ((c - 'a') << 4); c = str.charAt(i + 1);/*w w w . ja v a 2 s . c om*/ bytes[i / 2] += (c - 'a'); } return bytes; }
From source file:Main.java
public static char binaryBlockOf4ToHex(String input) { int sum = 0;// w w w . java 2 s . co m int first = (input.charAt(0) - 48) * 8; int second = (input.charAt(1) - 48) * 4; int third = (input.charAt(2) - 48) * 2; int fourth = (input.charAt(3) - 48); sum = first + second + third + fourth; if (sum == 10) return 'A'; else if (sum == 11) return 'B'; else if (sum == 12) return 'C'; else if (sum == 13) return 'D'; else if (sum == 14) return 'E'; else if (sum == 15) return 'F'; else return (char) (sum + 48); }
From source file:Main.java
public static boolean isUpperCase(String s) { int len = s.length(); for (int i = 0; i < len; i++) { char ch = s.charAt(i); if (Character.toUpperCase(ch) != ch) return false; }/*from w w w. ja v a 2s . co m*/ return len != 0; }
From source file:Main.java
/** * Tests if all characters are XML space (<tt>'\t'</tt>, <tt>'\r'</tt>, * <tt>'\n'</tt>, <tt>' '</tt>). * /* w w w . ja v a 2 s. co m*/ * @param s characters to be tested * @return <code>true</code> if test is successful; <code>false</code> * otherwise */ public static final boolean isXMLSpace(String s) { for (int c = s.length(); --c >= 0;) if (!isXMLSpace(s.charAt(c))) return false; return true; }
From source file:com.omnigon.aem.common.utils.StringUtils.java
public static String shortenString(String src, int length, boolean addEllipses) { if (isNotBlank(src) && src.length() > length) { if (addEllipses) { String sub = src.substring(0, length); char last = sub.charAt(sub.length() - 1); return PUNCTUATION.contains(Character.toString(last)) ? sub : sub + ELLIPSES; } else {//from ww w .j a v a 2 s . c o m return src.substring(0, length); } } return src; }
From source file:Main.java
public static String getRandomString(int length) { String retVal = "1234567890"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) sb.append(retVal.charAt(random.nextInt(retVal.length()))); return sb.toString(); }
From source file:Main.java
public static int hex2color(String color) { if (TextUtils.isEmpty(color)) { color = "#ffffff"; }// w ww .j a va2s . co m int result; if (color.charAt(0) != '#') { color = "#" + color; } result = Color.parseColor(color); return result; }
From source file:Main.java
private static String camelCase2Upper(String s) { String r = ""; boolean u = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); char C = Character.toUpperCase(c); if (Character.isUpperCase(c)) { r = r + (u ? ("_" + C) : C); } else {/* w w w . j a va2s . c o m*/ u = true; r = r + C; } } return r; }