方便咱們對字符串的操做java
import org.apache.commons.lang.StringUtils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 字符串處理工具類 * Created on 2018/7/3. * * @author wangzf */ public class StringUtil extends StringUtils { public static final Character TRUE_CHARACTER = '1'; /** * 是否爲真 * * @param c 字符串 * @return */ public static boolean isTrue(Character c) { if (TRUE_CHARACTER.equals(c)) { return true; } return false; } /** * 是否爲真 * * @param s 字符 * @return */ public static boolean isTrue(String s) { if (TRUE_CHARACTER.equals(s)) { return true; } return false; } /** * 追加雙引號 * * @param str 字符 * @return 追加後字符 */ public static String quote(String str) { return str == null ? null : (new StringBuilder('"')).append(str).append('"').toString(); } /** * 追加單引號 * * @param str 字符 * @return 追加後字符 */ public static String singleQuote(String str) { return str == null ? null : (new StringBuilder("'")).append(str).append("'").toString(); } /** * 替換文本 * * @param text 處理文本 * @param placeholder 佔位符 * @param parameters 替換參數 * @return 替換後文本 */ public static String replace(String text, String placeholder, String[] parameters) { if (!StringUtils.isEmpty(text) && !StringUtils.isEmpty(placeholder) && parameters != null) { for (String parameter : parameters) { text = replaceOnce(text, placeholder, parameter); } } return text; } /** * 替換文本 * * @param text 處理文本 * @param placeholder 佔位符 * @param parameters 替換參數 * @return 替換後文本 */ public static String replace(String text, String placeholder, List<String> parameters) { if (parameters != null && parameters.size() > 0) { return replace(text, placeholder, parameters.toArray(new String[parameters.size()])); } return text; } /** * @param str * @return */ public static String replaceColonToDot(String str) { if (StringUtils.isEmpty(str)) return str; return str.replaceAll(":", "."); } /** * 取得指定長度的數字字符組合 * * @param length 長度 * @return 數字字符 */ public static String getRandomChar(int length) { String val = ""; Random random = new Random(); for (int i = 0; i < length; i++) { // 輸出字母仍是數字 String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 字符串 if ("char".equalsIgnoreCase(charOrNum)) { // 取得大寫字母仍是小寫字母 int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; val += (char) (choice + random.nextInt(26)); } else if ("num".equalsIgnoreCase(charOrNum)) { // 數字 val += String.valueOf(random.nextInt(10)); } } return val; } /** * 解析參數 * * @param str 字符串 * @param regex 表達式 * @return 解析後的參數列表 * @throws Exception 異常 */ public static List<String> parseParams(String str, String regex) throws Exception { List<String> params = new ArrayList<String>(); Matcher matcher = Pattern.compile(regex).matcher(str); while (matcher.find()) { params.add(matcher.group()); } return params; } /** * 將字符串轉換爲數組 * * @param str 字符串 * @param regex 表達式 * @return 解析後的參數列表 * @throws Exception 異常 */ public static List<String> stringsToList(String str, String regex) throws Exception { List<String> params = new ArrayList<String>(); String[] stringArray = str.split(regex); for (String s : stringArray) { params.add(s); } return params; } public static boolean isIn(String target, String... strings) { for (String str : strings) { if (target == str) { return true; } } return false; } public static boolean isEmpty(Object value) { if (value == null) return true; if (isEmpty(value.toString())) return true; return false; } public static boolean isEmpty(Collection collection) { return collection == null || collection.isEmpty(); } public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } /** * 去除空格 * * @param str str * @return 去除後字符 */ public static String trimAll(String str) { if (str != null) { str = StringUtils.replace(str, " ", StringUtils.EMPTY); } return str != null ? str.trim() : null; } /** * 驗證手機號碼格式 * * @param mobile * @return boolean */ public static boolean isMobileNO(String mobile) { if (isEmpty(mobile)) { return false; } Pattern p = Pattern.compile("^[1][0-9]{10}$");//Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); Matcher m = p.matcher(mobile); return m.matches(); } /** * 使用正則對時間字符串的合法性進行校驗 * * @param dateStr 將要被校驗合法性的時間字符串,格式:pattern * <p> * //日期格式yyyy * PatternsDict.date_y= (\d{4}); * //日期格式yyyy-mm * PatternsDict.date_ym= (\d{4})-(0\d{1}|1[0-2]); * //日期格式yyyy-mm-dd * PatternsDict.date_ymd= (\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01]); * //時間格式hh * PatternsDict.time_h=(0\d{1}|1\d{1}|2[0-3]); * //時間格式hh:mm * PatternsDict.time_hm=(0\d{1}|1\d{1}|2[0-3]):([0-5]\d{1}); * //時間格式hh:mm:ss * PatternsDict.time_hms=(0\d{1}|1\d{1}|2[0-3]):[0-5]\d{1}:([0-5]\d{1}); */ public static boolean checkDateFormat(String dateStr, String patternStr) { if (StringUtil.isEmpty(dateStr) || StringUtil.isEmpty(patternStr)) { return false; } Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(dateStr); return matcher.matches(); } public static String stringList2SplitedString(List<String> srcList, String patternStr) { StringBuilder target = new StringBuilder(); if (null == srcList) { if (srcList.size() > 0) { if (srcList.size() <= 1) { target.append(srcList.get(0)); return target.toString(); } else { for (String s : srcList) { target.append(s); target.append(patternStr); } return target.toString().substring(0, target.toString().length() - 1); } } } return null; } }