Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.utils; //from ww w . j a va2s .com import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Locale; import java.util.Map; import java.util.UUID; import java.util.regex.Pattern; /** * ??????????????? * * @author Morgan.Ji * */ public class StrUtils { private final static Pattern EMAIL_PATTERN = Pattern .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); public static String encodeUrl(String requestUrl, Map<String, Object> params) { StringBuilder url = new StringBuilder(requestUrl); if (url.indexOf("?") < 0) url.append('?'); for (String name : params.keySet()) { url.append('&'); url.append(name); url.append('='); try { url.append(URLEncoder.encode(String.valueOf(params.get(name)), "utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return url.toString().replace("?&", "?"); } /** * ?????????URL?????{}?????????? * * @param url * @param parameter * @return */ public static String Format(String url, Object... parameter) { String result = url; for (int i = 0; i < parameter.length; i++) { int startPoint = -1; int endPoint = -1; startPoint = result.indexOf('{'); endPoint = result.indexOf('}'); if (startPoint >= 0 && endPoint > startPoint) { result = result.substring(0, startPoint) + parameter[i].toString() + result.substring(endPoint + 1); } } return result; } /** * ??????????????? ???????????????????????????????? ???????null????????true * * @param input * @return boolean */ public static boolean isEmpty(String input) { if (input == null || "".equals(input)) { return true; } for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c != ' ' && c != '\t' && c != '\r' && c != '\n') { return false; } } return true; } private static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; } // ?????????????? public static boolean isChinese(String strName) { char[] ch = strName.toCharArray(); for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (isChinese(c)) { return true; } } return false; } public static String generateId() { UUID uuid = UUID.randomUUID(); String uniqueId = uuid.toString(); return uniqueId; } public static String encryptToMD5(String info) { byte[] digesta = null; try { // ????md5???????? MessageDigest alga = MessageDigest.getInstance("MD5"); // ???????????????? alga.update(info.getBytes()); // ?????? digesta = alga.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } // ????????? String rs = byte2hex(digesta); return rs; } public static String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(Locale.CHINESE); } /** * ?????????????????????? * * @param email * @return */ public static boolean isEmail(String email) { if (email == null || email.trim().length() == 0) return false; return EMAIL_PATTERN.matcher(email).matches(); } }