| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.lib_coremodel.util; |
| | | |
| | | import android.text.TextUtils; |
| | | |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.text.DecimalFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | import java.util.Random; |
| | | import java.util.Set; |
| | | import java.util.UUID; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.Mac; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | |
| | | /** |
| | | * å符串æä½å·¥å
·å
|
| | | */ |
| | | public class StringUtil { |
| | | public final static String EMPTY=""; |
| | | private final static Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); |
| | | private final static Pattern identityCard = Pattern.compile("(\\d{14}[0-9xX])|(\\d{17}[0-9xX])"); |
| | | private final static Pattern passer = Pattern.compile("[A-Za-z0-9@._-]{6,20}"); |
| | | private final static Pattern pureNumber = Pattern.compile("[0-9]{8,30}"); |
| | | private final static Pattern phoner = Pattern.compile("1[0-9]{10}"); |
| | | private final static Pattern bankCarder = Pattern.compile("[0-9]{16}|[0-9]{19}"); |
| | | private final static Pattern user = Pattern.compile("[A-Za-z0-9@._-]{6,20}"); |
| | | private final static Pattern Age = Pattern.compile("[0-9]*"); |
| | | |
| | | private final static Pattern Chinese = Pattern.compile("^[\u4e00-\u9fa5]*$"); |
| | | private final static Pattern English = Pattern.compile("^[A-Za-z]+$"); |
| | | |
| | | private final static Pattern LinkMan = Pattern.compile("^([A-Za-z]+)$|^([\u4e00-\u9fa5]{2,4})$"); |
| | | private final static Pattern LoanAmt = Pattern.compile("\\d{1,10}"); |
| | | private final static Pattern LoanExpiry = Pattern.compile("^\\d{1,2}$"); |
| | | private final static Pattern BizLicense = Pattern.compile("(?!^[a-zA-Z]+$)[0-9a-zA-Z]+"); |
| | | private final static Pattern LoanMoney = Pattern.compile("^(\\d{1,10})(\\.\\d{1,2})?$"); |
| | | |
| | | private final static Pattern HouseSize = Pattern.compile("^(\\d{1,4})(\\.\\d{1,2})?$"); |
| | | private final static Pattern ZonuleName = Pattern.compile("[A-Za-z0-9\u4e00-\u9fa5]+"); |
| | | |
| | | private final static Pattern Color = Pattern.compile("[A-Za-z\u4e00-\u9fa5]+"); |
| | | |
| | | private final static Pattern PaiLiang = Pattern.compile("^(\\d{1,2})(\\.\\d{1,2})?$"); |
| | | private final static Pattern CarMileage = Pattern.compile("^(\\d{1,8})(\\.\\d{1,2})?$"); |
| | | |
| | | private final static Pattern Money = Pattern.compile("^(\\d{1,8})(\\.\\d{1,2})?$"); |
| | | |
| | | /** |
| | | * 夿ç»å®å符串æ¯å¦ç©ºç½ä¸²ã |
| | | * 空ç½ä¸²æ¯æç±ç©ºæ ¼ãå¶è¡¨ç¬¦ãåè½¦ç¬¦ãæ¢è¡ç¬¦ç»æçå符串 |
| | | * è¥è¾å
¥å符串为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; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³ç身份è¯å· |
| | | * |
| | | * @param card |
| | | * @return |
| | | */ |
| | | public static boolean isIdentityCard(String card) { |
| | | |
| | | if (card == null || "".equals(card)) |
| | | return false; |
| | | return identityCard.matcher(card).matches(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³ççµåé®ä»¶å°å |
| | | * |
| | | * @param email |
| | | * @return |
| | | */ |
| | | public static boolean isEmail(String email) { |
| | | if (TextUtils.isEmpty(email)) |
| | | return false; |
| | | return emailer.matcher(email).matches(); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³çç¨æ·å¯ç |
| | | * |
| | | * @return |
| | | */ |
| | | public static boolean isPassword(String password) { |
| | | if (TextUtils.isEmpty(password)) |
| | | return false; |
| | | return passer.matcher(password).matches(); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³çç¨æ·å |
| | | * |
| | | * @param custName |
| | | * @return trueä¸ºåæ³çç¨æ·å |
| | | */ |
| | | public static boolean isUser(String custName) { |
| | | |
| | | if (custName == null || "".equals(custName)) { |
| | | return false; |
| | | } |
| | | return user.matcher(custName).matches(); |
| | | |
| | | } |
| | | |
| | | public static boolean ispureNumber(String custName) { |
| | | return pureNumber.matcher(custName).matches(); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³çå¹´é¾ |
| | | * |
| | | * @param age |
| | | * @return |
| | | */ |
| | | public static boolean isAge(String age) { |
| | | return Age.matcher(age).matches(); |
| | | } |
| | | |
| | | /** |
| | | * è¿ä¸ªæ¹æ³åªæ¯ææå¤§é¿åº¦ä¸º32çéæºå符串,å¦è¦æ¯ææ´å¤§é¿åº¦çï¼å¯ä»¥éå½ä¿®æ¹æ¤æ¹æ³ï¼å¦åé¢è¡¥ãåé¢è¡¥ï¼æè
å¤ä¸ªuuidç¸è¿æ¥ |
| | | * @param length |
| | | * @return |
| | | */ |
| | | public static String toFixedLengthStringByUUID(int length) { |
| | | |
| | | //ä¹å¯ä»¥éè¿UUIDæ¥éæºçæ |
| | | UUID uuid = UUID.randomUUID(); |
| | | return uuid.toString().replace("-", "").substring(0, length); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³çææºå·ç |
| | | */ |
| | | public static boolean isPhone(String phone) { |
| | | if (TextUtils.isEmpty(phone)) |
| | | return false; |
| | | return phoner.matcher(phone).matches(); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯ä¸æ¯ä¸ä¸ªåæ³çé¶è¡å¡å· |
| | | * |
| | | * @return |
| | | */ |
| | | public static boolean isBankCard(String cardNum) { |
| | | if (cardNum == null || "".equals(cardNum)) |
| | | return false; |
| | | return bankCarder.matcher(cardNum).matches(); |
| | | } |
| | | |
| | | /** |
| | | * æ°åç±»åçé颿 ¼å¼åæå符串 |
| | | */ |
| | | public static String toMoney(Double obj) { |
| | | if (obj == null) { |
| | | return "0.00"; |
| | | } |
| | | DecimalFormat df = new DecimalFormat("#,#0.00"); |
| | | return df.format(obj); |
| | | } |
| | | |
| | | /** |
| | | * æ°åæ ¼å¼åï¼ä¿ç两ä½å°æ° |
| | | * |
| | | * @param obj |
| | | * @return |
| | | */ |
| | | public static String douFormat(double obj) { |
| | | DecimalFormat df = new DecimalFormat("0.00"); |
| | | return df.format(obj); |
| | | } |
| | | |
| | | /** |
| | | * è·åè¾å
¥çå符é¿åº¦ï¼æ±å为两个å符 |
| | | * |
| | | * @param value |
| | | * @return |
| | | */ |
| | | public static int stringLength(String value) { |
| | | int valueLength = 0; |
| | | String chinese = "[\u4e00-\u9fa5]"; |
| | | for (int i = 0; i < value.length(); i++) { |
| | | String temp = value.substring(i, i + 1); |
| | | if (temp.matches(chinese)) { |
| | | valueLength += 2; |
| | | } else { |
| | | valueLength += 1; |
| | | } |
| | | } |
| | | return valueLength; |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转å为doubleç±»å |
| | | * |
| | | * @param value |
| | | * @return |
| | | */ |
| | | public static double StringToDouble(String value) { |
| | | try { |
| | | if (value == null || "".equals(value)) |
| | | return 0.00; |
| | | return Double.parseDouble(value); |
| | | }catch (NumberFormatException e){ |
| | | return 0; |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * åç¬¦ä¸²è½¬æ´æ° |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static int StringToInt(String str) { |
| | | try { |
| | | return Integer.parseInt(str); |
| | | } catch (Exception e) { |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * å符串转long |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static long StringToLong(String str) { |
| | | try { |
| | | return Long.parseLong(str); |
| | | } catch (Exception e) { |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 夿å符串æ¯å¦æ¯ä¸æå符 |
| | | * |
| | | * @return boolean false åç¬¦ä¸²å«æå
¶ä»é䏿å符 true åç¬¦ä¸²é½æ¯ä¸æå符串 |
| | | */ |
| | | public static boolean isChinese(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | |
| | | for (int i = 0; i < str.length(); i++) { |
| | | |
| | | if (!isChinese(str.charAt(i))) { |
| | | return false; |
| | | } |
| | | |
| | | } |
| | | |
| | | return true; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 夿å符æ¯å¦æ¯ä¸æå符 |
| | | * |
| | | * @return boolean false åç¬¦ä¸æ¯ä¸æå符 true å符æ¯ä¸æå符 |
| | | */ |
| | | public static boolean isChinese(char c) { |
| | | |
| | | int v = (int) c; |
| | | return (v >= 19968 && v < 171941); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 䏿æ±å |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isC(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return Chinese.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * è±æ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isEnglish(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return English.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * éé¢ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isMoney(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return Money.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 夿å忝å¦å«æè±æç¹æ®å符ï¼åè½åºæªå®åï¼åºè¯¥å
å«ä¸æç¹æ®åç¬¦ï¼ |
| | | * |
| | | * @return boolean false ååä¸åæ³ï¼å«æè±æç¹æ®å符 true åååæ³ ï¼ä¸å«æè±æç¹æ®å符 |
| | | */ |
| | | public static boolean isNameLegal(String str) { |
| | | |
| | | String temp = "@_\\_|_~_`_!_#_$_%_^_&_*_(_)_{_}_[_]_;_:_'_\"_,_<_._>_/_?_-_ï¿¥_ï¼_ @_#_ï¿¥_%_&_*_ï¼_ï¼_ââ_+_ï¼_~_·_ï¼_ï¼|ã_ï½_ï½_ã_ã_1_2_3_4_5_6_7_8_9_0_â_â_â¦_ã_:_ã_ã_â_â¬_£_=_÷_ï¼_ã"; |
| | | String[] strs = temp.split("_"); |
| | | |
| | | for (int i = 0; i < strs.length; i++) { |
| | | |
| | | if (str.contains(strs[i])) { |
| | | return false; |
| | | } |
| | | |
| | | } |
| | | return true; |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 夿æ¯å¦ç¬¦åèç³»äººæ ¼å¼ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isLinkMan(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return LinkMan.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * èèµéé¢ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isLoanAmt(String str) { |
| | | |
| | | if (TextUtils.isEmpty(str)) { |
| | | return false; |
| | | } |
| | | return LoanAmt.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 贷款æé |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isLoanExpiry(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return LoanExpiry.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è¥ä¸æ§ç
§ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isBizLicense(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return BizLicense.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * èµé |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isLoanMoney(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return LoanMoney.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ¿å±é¢ç§¯ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isHouseSize(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return HouseSize.matcher(str).matches(); |
| | | } |
| | | |
| | | public static String cutOffCardNum(String id) { |
| | | if (id.length() == 18) { |
| | | return new StringBuilder().append(id.substring(0, 3)).append("****").append(id.substring(14, 18)).toString(); |
| | | } else if (id.length() == 15) { |
| | | return new StringBuilder().append(id.substring(0, 3)).append("****").append(id.substring(11, 15)).toString(); |
| | | } else { |
| | | return id; |
| | | } |
| | | } |
| | | |
| | | public static String cutOffPhone(String phone) { |
| | | if (isPhone(phone)) { |
| | | return new StringBuilder().append(phone.substring(0, 3)).append("****").append(phone.substring(8, 11)).toString(); |
| | | } else { |
| | | return phone; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å°åºåç§° |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isZonuleName(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return ZonuleName.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æé |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isPaiLiang(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return PaiLiang.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 汽车é¢è² |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isColor(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return Color.matcher(str).matches(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 汽车éç¨ |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static boolean isCarMileage(String str) { |
| | | |
| | | if (null == str || "".equals(str.trim())) { |
| | | return false; |
| | | } |
| | | return CarMileage.matcher(str).matches(); |
| | | } |
| | | |
| | | |
| | | public static String replaceEach(final String text, final String[] searchList, final String[] replacementList) { |
| | | return replaceEach(text, searchList, replacementList, false, 0); |
| | | } |
| | | |
| | | public static String dateFormat(String date){ |
| | | if(TextUtils.isEmpty(date)){ |
| | | return ""; |
| | | } |
| | | return date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8)+" "+date.substring(8,10)+":"+date.substring(10,12)+":"+date.substring(12); |
| | | } |
| | | |
| | | public static String simpleDateFormat(String date){ |
| | | if(date==null){ |
| | | return ""; |
| | | } |
| | | return date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8); |
| | | } |
| | | |
| | | public static String timeToString(long time){ |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String d = format.format(new Date(time)); |
| | | return d; |
| | | } |
| | | |
| | | /** |
| | | * <p> |
| | | * Replaces all occurrences of Strings within another String. |
| | | * </p> |
| | | * <p/> |
| | | * <p> |
| | | * A {@code null} reference passed to this method is a no-op, or if |
| | | * any "search string" or "string to replace" is null, that replace will be |
| | | * ignored. |
| | | * </p> |
| | | * <p/> |
| | | * <pre> |
| | | * StringUtils.replaceEachRepeatedly(null, *, *) = null |
| | | * StringUtils.replaceEachRepeatedly("", *, *) = "" |
| | | * StringUtils.replaceEachRepeatedly("aba", null, null) = "aba" |
| | | * StringUtils.replaceEachRepeatedly("aba", new String[0], null) = "aba" |
| | | * StringUtils.replaceEachRepeatedly("aba", null, new String[0]) = "aba" |
| | | * StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, null) = "aba" |
| | | * StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}) = "b" |
| | | * StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}) = "aba" |
| | | * StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte" |
| | | * (example of how it repeats) |
| | | * StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "tcte" |
| | | * StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}) = IllegalStateException |
| | | * </pre> |
| | | * |
| | | * @param text text to search and replace in, no-op if null |
| | | * @param searchList the Strings to search for, no-op if null |
| | | * @param replacementList the Strings to replace them with, no-op if null |
| | | * @return the text with any replacements processed, {@code null} if |
| | | * null String input |
| | | * @throws IllegalStateException if the search is repeating and there is an endless loop due |
| | | * to outputs of one being inputs to another |
| | | * @throws IllegalArgumentException if the lengths of the arrays are not the same (null is ok, |
| | | * and/or size 0) |
| | | * @since 2.4 |
| | | */ |
| | | public static String replaceEach( |
| | | final String text, final String[] searchList, final String[] replacementList, final boolean repeat, final int timeToLive) { |
| | | |
| | | // mchyzer Performance note: This creates very few new objects (one major goal) |
| | | // let me know if there are performance requests, we can create a harness to measure |
| | | |
| | | if (text == null || text.isEmpty() || searchList == null || |
| | | searchList.length == 0 || replacementList == null || replacementList.length == 0) { |
| | | return text; |
| | | } |
| | | |
| | | // if recursing, this shouldn't be less than 0 |
| | | if (timeToLive < 0) { |
| | | throw new IllegalStateException("Aborting to protect against StackOverflowError - " + |
| | | "output of one loop is the input of another"); |
| | | } |
| | | |
| | | final int searchLength = searchList.length; |
| | | final int replacementLength = replacementList.length; |
| | | |
| | | // make sure lengths are ok, these need to be equal |
| | | if (searchLength != replacementLength) { |
| | | throw new IllegalArgumentException("Search and Replace array lengths don't match: " |
| | | + searchLength |
| | | + " vs " |
| | | + replacementLength); |
| | | } |
| | | |
| | | // keep track of which still have matches |
| | | final boolean[] noMoreMatchesForReplIndex = new boolean[searchLength]; |
| | | |
| | | // index on index that the match was found |
| | | int textIndex = -1; |
| | | int replaceIndex = -1; |
| | | int tempIndex = -1; |
| | | |
| | | // index of replace array that will replace the search string found |
| | | // NOTE: logic duplicated below START |
| | | for (int i = 0; i < searchLength; i++) { |
| | | if (noMoreMatchesForReplIndex[i] || searchList[i] == null || |
| | | searchList[i].isEmpty() || replacementList[i] == null) { |
| | | continue; |
| | | } |
| | | tempIndex = text.indexOf(searchList[i]); |
| | | |
| | | // see if we need to keep searching for this |
| | | if (tempIndex == -1) { |
| | | noMoreMatchesForReplIndex[i] = true; |
| | | } else { |
| | | if (textIndex == -1 || tempIndex < textIndex) { |
| | | textIndex = tempIndex; |
| | | replaceIndex = i; |
| | | } |
| | | } |
| | | } |
| | | // NOTE: logic mostly below END |
| | | |
| | | // no search strings found, we are done |
| | | if (textIndex == -1) { |
| | | return text; |
| | | } |
| | | |
| | | int start = 0; |
| | | |
| | | // get a good guess on the size of the result buffer so it doesn't have to double if it goes over a bit |
| | | int increase = 0; |
| | | |
| | | // count the replacement text elements that are larger than their corresponding text being replaced |
| | | for (int i = 0; i < searchList.length; i++) { |
| | | if (searchList[i] == null || replacementList[i] == null) { |
| | | continue; |
| | | } |
| | | final int greater = replacementList[i].length() - searchList[i].length(); |
| | | if (greater > 0) { |
| | | increase += 3 * greater; // assume 3 matches |
| | | } |
| | | } |
| | | // have upper-bound at 20% increase, then let Java take over |
| | | increase = Math.min(increase, text.length() / 5); |
| | | |
| | | final StringBuilder buf = new StringBuilder(text.length() + increase); |
| | | |
| | | while (textIndex != -1) { |
| | | |
| | | for (int i = start; i < textIndex; i++) { |
| | | buf.append(text.charAt(i)); |
| | | } |
| | | buf.append(replacementList[replaceIndex]); |
| | | |
| | | start = textIndex + searchList[replaceIndex].length(); |
| | | |
| | | textIndex = -1; |
| | | replaceIndex = -1; |
| | | tempIndex = -1; |
| | | // find the next earliest match |
| | | // NOTE: logic mostly duplicated above START |
| | | for (int i = 0; i < searchLength; i++) { |
| | | if (noMoreMatchesForReplIndex[i] || searchList[i] == null || |
| | | searchList[i].isEmpty() || replacementList[i] == null) { |
| | | continue; |
| | | } |
| | | tempIndex = text.indexOf(searchList[i], start); |
| | | |
| | | // see if we need to keep searching for this |
| | | if (tempIndex == -1) { |
| | | noMoreMatchesForReplIndex[i] = true; |
| | | } else { |
| | | if (textIndex == -1 || tempIndex < textIndex) { |
| | | textIndex = tempIndex; |
| | | replaceIndex = i; |
| | | } |
| | | } |
| | | } |
| | | // NOTE: logic duplicated above END |
| | | |
| | | } |
| | | final int textLength = text.length(); |
| | | for (int i = start; i < textLength; i++) { |
| | | buf.append(text.charAt(i)); |
| | | } |
| | | final String result = buf.toString(); |
| | | if (!repeat) { |
| | | return result; |
| | | } |
| | | |
| | | return replaceEach(result, searchList, replacementList, repeat, timeToLive - 1); |
| | | } |
| | | |
| | | /** |
| | | * åè§è½¬å
¨è§ * @param |
| | | input String. * @return |
| | | å
¨è§å符串. */ |
| | | public static String toSBC(String input) { |
| | | char c[] = input.toCharArray(); |
| | | for (int i = 0; i < c.length; i++) { |
| | | if(c[i] == ' ') { |
| | | c[i] = '\u3000'; |
| | | }else if(c[i] < '\177') { |
| | | c[i] = (char) (c[i] + 65248); |
| | | } |
| | | } |
| | | return new String(c); |
| | | } |
| | | |
| | | public static String hashToStr(HashMap<String, Object> map){ |
| | | JSONObject clientKey = new JSONObject(); |
| | | // å°mapä¸çkey-valueå
³ç³»åå
¥å°setéåä¸ï¼å使ç¨Map.Entry |
| | | Set entrySet = map.entrySet(); // key-valueçsetéå |
| | | Iterator it2 = entrySet.iterator(); |
| | | while(it2.hasNext()){ |
| | | Map.Entry me = (Map.Entry) it2.next(); |
| | | Object k = me.getKey(); |
| | | Object v = me.getValue(); |
| | | try { |
| | | clientKey.put((String)k,v); |
| | | } catch (JSONException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return String.valueOf(clientKey); |
| | | } |
| | | |
| | | /** |
| | | * ç§è½¬ä¸ºåéå°æ¶ |
| | | * @return |
| | | *//* |
| | | public static String sToMH(int s) { |
| | | int h=0; |
| | | int m=0; |
| | | StringBuilder builder=new StringBuilder(); |
| | | if(s/3600>0){ |
| | | h=s/3600; |
| | | builder.append(h+MApplication.mContext.getResources().getString(R.string.time_h)); |
| | | s-=3600*h; |
| | | } |
| | | if(s/60>0){ |
| | | m=s/60; |
| | | builder.append(m+MApplication.mContext.getResources().getString(R.string.time_m)); |
| | | s-=m*60; |
| | | }else { |
| | | if(h!=0){ |
| | | builder.append(0+MApplication.mContext.getResources().getString(R.string.time_m)); |
| | | } |
| | | } |
| | | builder.append(s+ MApplication.mContext.getResources().getString(R.string.time_s)); |
| | | return builder.toString(); |
| | | }*/ |
| | | |
| | | /** |
| | | * å½±èææºä¸é´4ä½ |
| | | * @return |
| | | */ |
| | | public static String phoneToHind(String s) { |
| | | if(s==null){ |
| | | return ""; |
| | | } |
| | | if(s.length()==11){ |
| | | s=s.substring(0,3)+"****"+s.substring(7); |
| | | } |
| | | return s; |
| | | } |
| | | |
| | | /** |
| | | * å½±èé¶è¡å¡å·ç ï¼ä¿çå4ä½ |
| | | * @return |
| | | */ |
| | | public static String bankNumToHind(String s) { |
| | | if(s==null){ |
| | | return ""; |
| | | } |
| | | if(s.length()>4){ |
| | | int size=s.length(); |
| | | String s1=s.substring(size-4); |
| | | StringBuffer buffer=new StringBuffer(); |
| | | for(int i=0;i<size-4;i++){ |
| | | buffer.append("*"); |
| | | } |
| | | s=buffer.toString()+s1; |
| | | } |
| | | return s; |
| | | } |
| | | |
| | | /** |
| | | * SHAå å¯ |
| | | * @return å å¯ä¹åç坿 |
| | | */ |
| | | public static String shaEncrypt(String data) { |
| | | byte[] datas=data.getBytes(); |
| | | String result = null; |
| | | try { |
| | | //æ ¹æ®ç»å®çåèæ°ç»æé ä¸ä¸ªå¯é¥,第äºåæ°æå®ä¸ä¸ªå¯é¥ç®æ³çåç§° |
| | | SecretKeySpec signinKey = new SecretKeySpec("Gu5t9xGARNpq86cd98joQYCN3Cozk1qA".getBytes(), "HmacSHA1"); |
| | | //çæä¸ä¸ªæå® Mac ç®æ³ ç Mac 对象 |
| | | Mac mac = Mac.getInstance("HmacSHA1"); |
| | | //ç¨ç»å®å¯é¥åå§å Mac 对象 |
| | | mac.init(signinKey); |
| | | //宿 Mac æä½ |
| | | byte[] rawHmac = mac.doFinal(datas); |
| | | result = new String(); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | System.err.println(e.getMessage()); |
| | | } catch (Exception e) { |
| | | System.err.println(e.getMessage()); |
| | | } |
| | | if (null != result) { |
| | | return result; |
| | | } else { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * byteæ°ç»è½¬æ¢ä¸º16è¿å¶å符串 |
| | | * |
| | | * @param bts |
| | | * æ°æ®æº |
| | | * @return 16è¿å¶å符串 |
| | | */ |
| | | public static String bytes2Hex(byte[] bts) { |
| | | String des = ""; |
| | | String tmp = null; |
| | | for (int i = 0; i < bts.length; i++) { |
| | | tmp = (Integer.toHexString(bts[i] & 0xFF)); |
| | | if (tmp.length() == 1) { |
| | | des += "0"; |
| | | } |
| | | des += tmp; |
| | | } |
| | | return des; |
| | | } |
| | | |
| | | // å å¯ |
| | | public static String encrypt(String sSrc){ |
| | | String sKey = "abcdef0123456789"; |
| | | String ivParameter = "0123456789abcdef"; |
| | | String result = ""; |
| | | try { |
| | | Cipher cipher; |
| | | cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| | | byte[] raw = sKey.getBytes(); |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
| | | IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使ç¨CBC模å¼ï¼éè¦ä¸ä¸ªåéivï¼å¯å¢å å å¯ç®æ³ç强度 |
| | | cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); |
| | | byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); |
| | | result = new String(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // æ¤å¤ä½¿ç¨BASE64å转ç ã |
| | | return result; |
| | | |
| | | } |
| | | |
| | | // è§£å¯ |
| | | public static String decrypt(String sSrc){ |
| | | if(sSrc==null){ |
| | | return ""; |
| | | } |
| | | String sKey = "abcdef0123456789"; |
| | | String ivParameter = "0123456789abcdef"; |
| | | try { |
| | | byte[] raw = sKey.getBytes("ASCII"); |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
| | | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| | | IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes()); |
| | | cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); |
| | | byte[] encrypted1 = "".getBytes();// å
ç¨base64è§£å¯ |
| | | byte[] original = cipher.doFinal(encrypted1); |
| | | String originalString = new String(original, "utf-8"); |
| | | raw=null; |
| | | encrypted1=null; |
| | | original=null; |
| | | return "{\"data\":"+originalString+"}"; |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | // è§£å¯ |
| | | public static String decryptNew(String sSrc){ |
| | | if(sSrc==null){ |
| | | return ""; |
| | | } |
| | | String sKey = "abcdef0123456789"; |
| | | String ivParameter = "0123456789abcdef"; |
| | | try { |
| | | byte[] raw = sKey.getBytes("ASCII"); |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
| | | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| | | IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes()); |
| | | cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); |
| | | byte[] encrypted1 = "".getBytes();// å
ç¨base64è§£å¯ |
| | | byte[] original = cipher.doFinal(encrypted1); |
| | | String originalString = new String(original, "utf-8"); |
| | | raw=null; |
| | | encrypted1=null; |
| | | original=null; |
| | | return originalString; |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | // è§£å¯ |
| | | public static String decryptNoData(String sSrc){ |
| | | if(sSrc==null){ |
| | | return ""; |
| | | } |
| | | String sKey = "abcdef0123456789"; |
| | | String ivParameter = "0123456789abcdef"; |
| | | try { |
| | | byte[] raw = sKey.getBytes("ASCII"); |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
| | | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| | | IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes()); |
| | | cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); |
| | | byte[] encrypted1 = "".getBytes();// å
ç¨base64è§£å¯ |
| | | byte[] original = cipher.doFinal(encrypted1); |
| | | String originalString = new String(original, "utf-8"); |
| | | raw=null; |
| | | encrypted1=null; |
| | | original=null; |
| | | return originalString+"}"; |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param str |
| | | * @return |
| | | * @throws NumberFormatException |
| | | */ |
| | | public static String inputPriceFormatter(String str)throws NumberFormatException { |
| | | String handledStr = null; |
| | | //å°æ°å½¢å¼çè¾å
¥ |
| | | if(isValid(str).contains(".")){ |
| | | float f_price = Float.valueOf(isValid(str)); |
| | | handledStr = Float.toString(f_price); |
| | | //æ´æ°å½¢å¼çè¾å
¥ |
| | | }else{ |
| | | int i_price = Integer.valueOf(isValid(str)); |
| | | handledStr = Integer.toString(i_price); |
| | | } |
| | | return isValid(handledStr); |
| | | } |
| | | /** |
| | | * 夿æ¯å¦ä¸ºç©ºå空串,以NumberFormatExceptionçæ¹å¼éä¸å¤ç |
| | | * @param str |
| | | * @return |
| | | * @throws NumberFormatException |
| | | */ |
| | | public static String isValid(String str) throws NumberFormatException { |
| | | if(str == null || "".equals(str)) |
| | | throw new NumberFormatException(); |
| | | return str; |
| | | } |
| | | |
| | | // get rand color |
| | | public static String getRandColorCode() { |
| | | String r,g,b; |
| | | Random random = new Random(); |
| | | r = Integer.toHexString(random.nextInt(256)).toUpperCase(); |
| | | g = Integer.toHexString(random.nextInt(256)).toUpperCase(); |
| | | b = Integer.toHexString(random.nextInt(256)).toUpperCase(); |
| | | |
| | | r = r.length() == 1 ? "0" + r : r ; |
| | | g = g.length() == 1 ? "0" + g : g ; |
| | | b = b.length() == 1 ? "0" + b : b ; |
| | | |
| | | return "0xFF"+r + g + b; |
| | | } |
| | | |
| | | public static boolean isCEN(String txt) { |
| | | |
| | | Pattern p = Pattern.compile("[0-9]*"); |
| | | Matcher m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | p = Pattern.compile("[a-zA-Z]"); |
| | | m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | p = Pattern.compile("[\u4e00-\u9fa5]"); |
| | | m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public static boolean isLetterOrNum(String txt) { |
| | | |
| | | Pattern p = Pattern.compile("[0-9]*"); |
| | | Matcher m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | p = Pattern.compile("[a-zA-Z]"); |
| | | m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public static boolean isNumber(String txt) { |
| | | Pattern p = Pattern.compile("[0-9]*"); |
| | | Matcher m = p.matcher(txt); |
| | | if (m.matches()) { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * æ¥æè½¬æ¢æJavaå符串 |
| | | * @param date |
| | | * @return str |
| | | */ |
| | | public static String DateToStr(Date date) { |
| | | |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String str = format.format(date); |
| | | return str; |
| | | } |
| | | |
| | | public static String DateToStrSS(Date date) { |
| | | |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); |
| | | String str = format.format(date); |
| | | return str; |
| | | } |
| | | |
| | | public static String getHM() { |
| | | |
| | | SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); |
| | | String str = format.format(new Date()); |
| | | return str; |
| | | } |
| | | |
| | | public static String DateToStrYMD(Date date) { |
| | | |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String str = format.format(date); |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * åç¬¦ä¸²è½¬æ¢ææ¥æ |
| | | * @param str |
| | | * @return date |
| | | */ |
| | | public static Date StrToDate(String str) { |
| | | |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date date = null; |
| | | try { |
| | | date = format.parse(str); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return date; |
| | | } |
| | | |
| | | public static String removeWhiteSpace(String inputStr) { |
| | | String outputStr = ""; |
| | | if (inputStr != null && !inputStr.isEmpty()) { |
| | | //ç§»é¤æææ¢è¡åç©ºæ ¼ |
| | | outputStr = inputStr.replaceAll("\\s+", ""); |
| | | } |
| | | return outputStr; |
| | | } |
| | | } |