From cdbaf79d124d46b69dc62b653fde55f9d40b5534 Mon Sep 17 00:00:00 2001 From: 陈奇 <1650699704@qq.com> Date: Thu, 01 Nov 2018 17:23:32 +0800 Subject: [PATCH] [*]修改电磁版本 --- andbrickslib/src/main/java/com/moral/andbrickslib/utils/StringUtils.java | 695 ++++++++++++++++++++++++++++++--------------------------- 1 files changed, 371 insertions(+), 324 deletions(-) diff --git a/andbrickslib/src/main/java/com/moral/andbrickslib/utils/StringUtils.java b/andbrickslib/src/main/java/com/moral/andbrickslib/utils/StringUtils.java index 538050f..d541ff0 100644 --- a/andbrickslib/src/main/java/com/moral/andbrickslib/utils/StringUtils.java +++ b/andbrickslib/src/main/java/com/moral/andbrickslib/utils/StringUtils.java @@ -13,261 +13,260 @@ /** * ������������������ - * + * * @author Administrator - * */ public class StringUtils { - private static final int MAX_PASSWORD_LENGTH = 16; - private static final int MIN_PASSWORD_LENGTH = 6; + private static final int MAX_PASSWORD_LENGTH = 16; + private static final int MIN_PASSWORD_LENGTH = 6; - public static final String URL_REG_EXPRESSION = "^(https?://)?([a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+)+(/*[A-Za-z0-9/\\-_&:?\\+=//.%]*)*"; + public static final String URL_REG_EXPRESSION = "^(https?://)?([a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+)+(/*[A-Za-z0-9/\\-_&:?\\+=//.%]*)*"; - /** - * is null or its length is 0 or it is made by space - * - * <pre> - * isBlank(null) = true; - * isBlank("") = true; - * isBlank(" ") = true; - * isBlank("a") = false; - * isBlank("a ") = false; - * isBlank(" a") = false; - * isBlank("a b") = false; - * </pre> - * - * @param str - * @return if string is null or its size is 0 or it is made by space, return true, else return false. - */ - public static boolean isBlank(String str) { - return (str == null || str.trim().length() == 0); - } + /** + * is null or its length is 0 or it is made by space + * + * <pre> + * isBlank(null) = true; + * isBlank("") = true; + * isBlank(" ") = true; + * isBlank("a") = false; + * isBlank("a ") = false; + * isBlank(" a") = false; + * isBlank("a b") = false; + * </pre> + * + * @param str + * @return if string is null or its size is 0 or it is made by space, return true, else return false. + */ + public static boolean isBlank(String str) { + return (str == null || str.trim().length() == 0); + } - /** - * is null or its length is 0 - * - * <pre> - * isEmpty(null) = true; - * isEmpty("") = true; - * isEmpty(" ") = false; - * </pre> - * - * @param str - * @return if string is null or its size is 0, return true, else return false. - */ - public static boolean isEmpty(String str) { - return (str == null || str.length() == 0) && isBlank(str); - } + /** + * is null or its length is 0 + * + * <pre> + * isEmpty(null) = true; + * isEmpty("") = true; + * isEmpty(" ") = false; + * </pre> + * + * @param str + * @return if string is null or its size is 0, return true, else return false. + */ + public static boolean isEmpty(String str) { + return (str == null || str.length() == 0) && isBlank(str); + } + /** + * ������������ + * + * @param mobileStr + * @param subStr + * @return + */ + public static String replacePhoneStr(String mobileStr, String subStr) { + String resultStr = ""; + if (resultStr != null) { + resultStr = mobileStr.substring(0, 3) + subStr + + mobileStr.substring(7, 11); + } + return resultStr; + } - /** - * ������������ - * - * @param mobileStr - * @param subStr - * @return - */ - public static String replacePhoneStr(String mobileStr, String subStr) { - String resultStr = ""; - if (resultStr != null) { - resultStr = mobileStr.substring(0, 3) + subStr - + mobileStr.substring(7, 11); - } - return resultStr; - } + /** + * ������UTF-8 + * + * @param urlString + * @return + */ + public static String decodeUTF8(String urlString) { + return decodeUrl(urlString.replaceAll("%", ""), "utf-8"); + } - /** - * ������UTF-8 - * @param urlString - * @return - */ - public static String decodeUTF8(String urlString) { - return decodeUrl(urlString.replaceAll("%", ""), "utf-8"); - } - public static String decodeUrl(String urlString, String decode) { - String decodeStr = ""; - if (urlString != null) { - try { - decodeStr = URLDecoder.decode(urlString, decode); - } catch (UnsupportedEncodingException e) { - } - } - return decodeStr; - } + public static String decodeUrl(String urlString, String decode) { + String decodeStr = ""; + if (urlString != null) { + try { + decodeStr = URLDecoder.decode(urlString, decode); + } catch (UnsupportedEncodingException e) { + } + } + return decodeStr; + } - /** - * ������UTF-8 - * @param str - * @return - */ - public static String utf8Encode(String str) { - if (!isEmpty(str) && str.getBytes().length != str.length()) { - try { - return URLEncoder.encode(str, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UnsupportedEncodingException occurred. ", e); - } - } - return str; - } + /** + * ������UTF-8 + * + * @param str + * @return + */ + public static String utf8Encode(String str) { + if (!isEmpty(str) && str.getBytes().length != str.length()) { + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("UnsupportedEncodingException occurred. ", e); + } + } + return str; + } - /** - * ������������������������������ - * - * @param string - * @return - */ - public static boolean isValidPassword(String string) { - boolean isValid = true; - if (string != null && !"".equals(string)) { - if (string.length() >= MIN_PASSWORD_LENGTH - && string.length() <= MAX_PASSWORD_LENGTH) { - int length = string.length(); - for (int i = 0; i < length; i++) { - int a = string.charAt(i); - if (a >= 32 && a <= 126) { - // do nothing - } else { - isValid = false; - } - } - } else { - isValid = false; - } - } else { - isValid = false; - } - return isValid; - } + /** + * ������������������������������ + * + * @param string + * @return + */ + public static boolean isValidPassword(String string) { + boolean isValid = true; + if (string != null && !"".equals(string)) { + if (string.length() >= MIN_PASSWORD_LENGTH + && string.length() <= MAX_PASSWORD_LENGTH) { + int length = string.length(); + for (int i = 0; i < length; i++) { + int a = string.charAt(i); + if (a >= 32 && a <= 126) { + // do nothing + } else { + isValid = false; + } + } + } else { + isValid = false; + } + } else { + isValid = false; + } + return isValid; + } - /** - * ������������������������������ - * - * @param address - * ������������ - * - * @return ������ - */ - public static boolean isEmailAddressLegal(String address) { - if (address - .matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) { - return true; - } - return false; - } + /** + * ������������������������������ + * + * @param address ������������ + * @return ������ + */ + public static boolean isEmailAddressLegal(String address) { + if (address + .matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) { + return true; + } + return false; + } - /** - * ������������������������������. - * - * @param str - * ������������������������������ - * @return ���������������������������������true���������false - */ - public static Boolean isMobilePhone(String str) { - Boolean isMobileNo = false; - try { - Pattern p = Pattern - .compile("^[1][0-9][0-9]{9}$"); - Matcher m = p.matcher(str); - isMobileNo = m.matches(); - } catch (Exception e) { - e.printStackTrace(); - } - return isMobileNo; - } + /** + * ������������������������������. + * + * @param str ������������������������������ + * @return ���������������������������������true���������false + */ + public static Boolean isMobilePhone(String str) { + Boolean isMobileNo = false; + try { + Pattern p = Pattern + .compile("^[1][0-9][0-9]{9}$"); + Matcher m = p.matcher(str); + isMobileNo = m.matches(); + } catch (Exception e) { + e.printStackTrace(); + } + return isMobileNo; + } - /** - * ������������������������������ - * - * @param account - * @return 0 success, other error 1 for length error 2 context error 3 - */ - public static byte isValidAccount(String account) { - byte _bReturnValue = 0; - String chinese = "[\u0391-\uFFE5]"; - String expr = "^[A-Za-z0-9_]+$"; - do { - if (account.length() < 2 || account.length() > 18) { - _bReturnValue = 1; - break; - } - // ������������ - String temp = ""; - for (int i = 0; i < account.length(); i++) { - // ������������������ - temp = account.substring(i, i + 1); - // ��������������������� - if (temp.matches(chinese)) - continue; - // ��������������������� ��������������������� - if (temp.matches(expr)) - continue; + /** + * ������������������������������ + * + * @param account + * @return 0 success, other error 1 for length error 2 context error 3 + */ + public static byte isValidAccount(String account) { + byte _bReturnValue = 0; + String chinese = "[\u0391-\uFFE5]"; + String expr = "^[A-Za-z0-9_]+$"; + do { + if (account.length() < 2 || account.length() > 18) { + _bReturnValue = 1; + break; + } + // ������������ + String temp = ""; + for (int i = 0; i < account.length(); i++) { + // ������������������ + temp = account.substring(i, i + 1); + // ��������������������� + if (temp.matches(chinese)) + continue; + // ��������������������� ��������������������� + if (temp.matches(expr)) + continue; - _bReturnValue = 2; + _bReturnValue = 2; - return _bReturnValue; - } - } while (false); + return _bReturnValue; + } + } while (false); - return _bReturnValue; - } + return _bReturnValue; + } - /** - * ������������������ - * - * @param fileS - * @return - */ - public static String FormetFileSize(long fileS) {// ������������������ - DecimalFormat df = new DecimalFormat("#0.0"); - String fileSizeString = ""; - if (fileS < 1024) { - fileSizeString = df.format((double) fileS) + "B"; - } else if (fileS < 1048576) { - fileSizeString = df.format((double) fileS / 1024) + "K"; - } else if (fileS < 1073741824) { - fileSizeString = df.format((double) fileS / 1048576) + "M"; - } else { - fileSizeString = df.format((double) fileS / 1073741824) + "G"; - } - return fileSizeString; - } + /** + * ������������������ + * + * @param fileS + * @return + */ + public static String FormetFileSize(long fileS) {// ������������������ + DecimalFormat df = new DecimalFormat("#0.0"); + String fileSizeString = ""; + if (fileS < 1024) { + fileSizeString = df.format((double) fileS) + "B"; + } else if (fileS < 1048576) { + fileSizeString = df.format((double) fileS / 1024) + "K"; + } else if (fileS < 1073741824) { + fileSizeString = df.format((double) fileS / 1048576) + "M"; + } else { + fileSizeString = df.format((double) fileS / 1073741824) + "G"; + } + return fileSizeString; + } - /** - * ��������������������� - * @param f - * @return - * @throws Exception - */ - public static long getFileSize(File f) throws Exception// ��������������������� - { - long size = 0; - File flist[] = f.listFiles(); - for (int i = 0; i < flist.length; i++) { - if (flist[i].isDirectory()) { - size = size + getFileSize(flist[i]); - } else { - size = size + flist[i].length(); - } - } - return size; - } + /** + * ��������������������� + * + * @param f + * @return + * @throws Exception + */ + public static long getFileSize(File f) throws Exception// ��������������������� + { + long size = 0; + File flist[] = f.listFiles(); + for (int i = 0; i < flist.length; i++) { + if (flist[i].isDirectory()) { + size = size + getFileSize(flist[i]); + } else { + size = size + flist[i].length(); + } + } + return size; + } - /** - * ���������������url - * - * @param s - * @return - */ - public static boolean isUrl(String s) { - if (s == null) { - return false; - } - return Pattern.matches(URL_REG_EXPRESSION, s); - } - - public static String getIMSI(Context context) { + /** + * ���������������url + * + * @param s + * @return + */ + public static boolean isUrl(String s) { + if (s == null) { + return false; + } + return Pattern.matches(URL_REG_EXPRESSION, s); + } + + public static String getIMSI(Context context) { try { if (context == null) { return ""; @@ -295,11 +294,13 @@ return ""; } - /** - * ��������������������� - * @param context - * @return - */ + + /** + * ��������������������� + * + * @param context + * @return + */ public static String getDeviceId(Context context) { try { String strIMEI = getIMEI(context); @@ -317,95 +318,141 @@ return ""; } - /** - * ��������������� - * @param bodyHTML - * @return - */ - public static String getHtmlData(String bodyHTML) { - String head = "<head>" - + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " - + "<style>img{max-width: 100%; width:auto; height:auto;}</style>" - + "</head>"; - return "<html>" + head + "<body>" + bodyHTML + "</body></html>"; - } + /** + * ��������������� + * + * @param bodyHTML + * @return + */ + public static String getHtmlData(String bodyHTML) { + String head = "<head>" + + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " + + "<style>img{max-width: 100%; width:auto; height:auto;}</style>" + + "</head>"; + return "<html>" + head + "<body>" + bodyHTML + "</body></html>"; + } - /** - * html���String - * @param source - * @return - */ - public static String htmlEscapeCharsToString(String source) { - return isEmpty(source) ? source : source.replaceAll("<", "<").replaceAll(">", ">") - .replaceAll("&", "&").replaceAll(""", "\""); - } + /** + * html���String + * + * @param source + * @return + */ + public static String htmlEscapeCharsToString(String source) { + return isEmpty(source) ? source : source.replaceAll("<", "<").replaceAll(">", ">") + .replaceAll("&", "&").replaceAll(""", "\""); + } - /** - * ���������ASCII��������������������������� ������"EF"���> 0xEF - * - * @param src0 - * byte - * @param src1 - * byte - * @return byte - */ - public static byte uniteBytes(byte src0, byte src1) { - byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue(); - _b0 = (byte) (_b0 << 4); - byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue(); - byte ret = (byte) (_b0 ^ _b1); - return ret; - } + /** + * ���������ASCII��������������������������� ������"EF"���> 0xEF + * + * @param src0 byte + * @param src1 byte + * @return byte + */ + public static byte uniteBytes(byte src0, byte src1) { + byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); + _b0 = (byte) (_b0 << 4); + byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); + byte ret = (byte) (_b0 ^ _b1); + return ret; + } - /** - * ������������������src������������������������������������16������������ ������"2B44EFD9" ���> byte[]{0x2B, 0��44, 0xEF, - * 0xD9} - * - * @param src - * String - * @return byte[] - */ - public static byte[] HexString2Bytes(String src) { - if (null == src || 0 == src.length()) { - return null; - } - byte[] ret = new byte[src.length() / 2]; - byte[] tmp = src.getBytes(); - for (int i = 0; i < (tmp.length / 2); i++) { - ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); - } - return ret; - } - public static String byteArrayToHexStr(byte[] byteArray) { - if (byteArray == null){ - return null; - } - char[] hexArray = "0123456789ABCDEF".toCharArray(); - char[] hexChars = new char[byteArray.length * 2]; - for (int j = 0; j < byteArray.length; j++) { - int v = byteArray[j] & 0xFF; - hexChars[j * 2] = hexArray[v >>> 4]; - hexChars[j * 2 + 1] = hexArray[v & 0x0F]; - } - return new String(hexChars); - } + /** + * ������������������src������������������������������������16������������ ������"2B44EFD9" ���> byte[]{0x2B, 0��44, 0xEF, + * 0xD9} + * + * @param src String + * @return byte[] + */ + public static byte[] HexString2Bytes(String src) { + if (null == src || 0 == src.length()) { + return null; + } + byte[] ret = new byte[src.length() / 2]; + byte[] tmp = src.getBytes(); + for (int i = 0; i < (tmp.length / 2); i++) { + ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); + } + return ret; + } - public static String str2HexStr(String str) - { + public static String byteArrayToHexStr(byte[] byteArray) { + if (byteArray == null) { + return null; + } + char[] hexArray = "0123456789ABCDEF".toCharArray(); + char[] hexChars = new char[byteArray.length * 2]; + for (int j = 0; j < byteArray.length; j++) { + int v = byteArray[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } - char[] chars = "0123456789ABCDEF".toCharArray(); - StringBuilder sb = new StringBuilder(""); - byte[] bs = str.getBytes(); - int bit; + public static String dumpHex(byte[] src) { + String num = "0123456789ABCDEF"; + StringBuilder sb = new StringBuilder(); +// sb.append("[ "); + for (byte aSrc : src) { + int high = aSrc >> 4 & 0x0f; + int low = aSrc & 0x0f; + sb.append(num.charAt(high)).append(num.charAt(low)).append(" "); + } +// sb.append(" ]"); + return sb.toString(); + } - for (int i = 0; i < bs.length; i++) - { - bit = (bs[i] & 0x0f0) >> 4; - sb.append(chars[bit]); - bit = bs[i] & 0x0f; - sb.append(chars[bit]); - sb.append(' '); - } - return sb.toString().trim(); - } + public static String str2HexStr(String str) { + + char[] chars = "0123456789ABCDEF".toCharArray(); + StringBuilder sb = new StringBuilder(""); + byte[] bs = str.getBytes(); + int bit; + + for (int i = 0; i < bs.length; i++) { + bit = (bs[i] & 0x0f0) >> 4; + sb.append(chars[bit]); + bit = bs[i] & 0x0f; + sb.append(chars[bit]); + sb.append(' '); + } + return sb.toString().trim(); + } + + /** + * ��������������������������� + * + * @param msg + * @param split + * @return + */ + public static String[] splitString(String msg, int split) { + msg = msg.replace(" ", ""); + String[] strings = new String[msg.length() / split]; + int j = 0; + for (int i = 0; i < msg.length(); i++) { + if (i % split == 1) { + strings[j++] = msg.substring(i - (split - 1), i + 1); + } + } + return strings; + } + + /** + * ��������������������� + * + * @param strings + * @param start + * @param end + * @return + */ + public static String subStrings(String[] strings, int start, int end) { + StringBuffer buffer = new StringBuffer(); + for (int i = start; i <= end; i++) { + buffer.append(strings[i]); + } + return buffer.toString(); + } } -- Gitblit v1.8.0