|
|
@@ -0,0 +1,701 @@
|
|
|
+package com.sckw.core.utils;
|
|
|
+
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description 自测公共处理类
|
|
|
+ * @Author zk
|
|
|
+ * @Date 2019/5/13 0013
|
|
|
+ */
|
|
|
+public class StringUtils {
|
|
|
+
|
|
|
+ /** 空字符串 */
|
|
|
+ private static final String NULLSTR = "";
|
|
|
+ /** 下划线 */
|
|
|
+ private static final char SEPARATOR_CHAR = '_';
|
|
|
+ /** 下划线 */
|
|
|
+ private static final String SEPARATOR_STRING = "_";
|
|
|
+ /** 加密字符 */
|
|
|
+ public static final String HASH_ALGORITHM = "SHA-1";
|
|
|
+ /**${xxx}**/
|
|
|
+ public static final Pattern pattern = Pattern.compile("(\\$\\{)([\\w]+)(\\})");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断字符串是否为空
|
|
|
+ * @param str 源字符串
|
|
|
+ * @author dengyinghui
|
|
|
+ * @date 2018/11/12
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean validatorEmpty(String str){
|
|
|
+ if(str != null && !"".equals(str)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判空操作(空)
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isBlank(Object str) {
|
|
|
+ String value = objectStr(str);
|
|
|
+ return value == null || "".equals(value) || "null".equals(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判空操作(空)
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isBlank(String value) {
|
|
|
+ return value == null || "".equals(value) || "null".equals(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判空操作(非空)
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isNotBlank(Object str) {
|
|
|
+ String value = objectStr(str);
|
|
|
+ return value != null && !"".equals(value) && !"null".equals(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判空操作(非空)
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isNotBlank(String value) {
|
|
|
+ return value != null && !"".equals(value) && !"null".equals(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取uuid
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String uuid() {
|
|
|
+ return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取参数不为空值
|
|
|
+ *
|
|
|
+ * @param value defaultValue 要判断的value
|
|
|
+ * @return value 返回值
|
|
|
+ */
|
|
|
+ public static <T> T nvl(T value, T defaultValue)
|
|
|
+ {
|
|
|
+ return value != null ? value : defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个Collection是否为空, 包含List,Set,Queue
|
|
|
+ *
|
|
|
+ * @param coll 要判断的Collection
|
|
|
+ * @return true:为空 false:非空
|
|
|
+ */
|
|
|
+ public static boolean isEmpty(Collection<?> coll)
|
|
|
+ {
|
|
|
+ return isNull(coll) || coll.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个Collection是否非空,包含List,Set,Queue
|
|
|
+ *
|
|
|
+ * @param coll 要判断的Collection
|
|
|
+ * @return true:非空 false:空
|
|
|
+ */
|
|
|
+ public static boolean isNotEmpty(Collection<?> coll)
|
|
|
+ {
|
|
|
+ return !isEmpty(coll);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个对象数组是否为空
|
|
|
+ *
|
|
|
+ * @param objects 要判断的对象数组
|
|
|
+ ** @return true:为空 false:非空
|
|
|
+ */
|
|
|
+ public static boolean isEmpty(Object[] objects)
|
|
|
+ {
|
|
|
+ return isNull(objects) || (objects.length == 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个对象数组是否非空
|
|
|
+ *
|
|
|
+ * @param objects 要判断的对象数组
|
|
|
+ * @return true:非空 false:空
|
|
|
+ */
|
|
|
+ public static boolean isNotEmpty(Object[] objects)
|
|
|
+ {
|
|
|
+ return !isEmpty(objects);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个Map是否为空
|
|
|
+ *
|
|
|
+ * @param map 要判断的Map
|
|
|
+ * @return true:为空 false:非空
|
|
|
+ */
|
|
|
+ public static boolean isEmpty(Map<?, ?> map)
|
|
|
+ {
|
|
|
+ return isNull(map) || map.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个Map是否为空
|
|
|
+ *
|
|
|
+ * @param map 要判断的Map
|
|
|
+ * @return true:非空 false:空
|
|
|
+ */
|
|
|
+ public static boolean isNotEmpty(Map<?, ?> map)
|
|
|
+ {
|
|
|
+ return !isEmpty(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个字符串是否为空串
|
|
|
+ *
|
|
|
+ * @param str String
|
|
|
+ * @return true:为空 false:非空
|
|
|
+ */
|
|
|
+ public static boolean isEmpty(String str)
|
|
|
+ {
|
|
|
+ return isNull(str) || NULLSTR.equals(str.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个字符串是否为非空串
|
|
|
+ *
|
|
|
+ * @param str String
|
|
|
+ * @return true:非空串 false:空串
|
|
|
+ */
|
|
|
+ public static boolean isNotEmpty(String str)
|
|
|
+ {
|
|
|
+ return !isEmpty(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个对象是否为空
|
|
|
+ *
|
|
|
+ * @param object Object
|
|
|
+ * @return true:为空 false:非空
|
|
|
+ */
|
|
|
+ public static boolean isNull(Object object)
|
|
|
+ {
|
|
|
+ return object == null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个对象是否非空
|
|
|
+ *
|
|
|
+ * @param object Object
|
|
|
+ * @return true:非空 false:空
|
|
|
+ */
|
|
|
+ public static boolean isNotNull(Object object)
|
|
|
+ {
|
|
|
+ return !isNull(object);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 判断一个对象是否是数组类型(Java基本型别的数组)
|
|
|
+ *
|
|
|
+ * @param object 对象
|
|
|
+ * @return true:是数组 false:不是数组
|
|
|
+ */
|
|
|
+ public static boolean isArray(Object object)
|
|
|
+ {
|
|
|
+ return isNotNull(object) && object.getClass().isArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 去空格
|
|
|
+ */
|
|
|
+ public static String trim(String str)
|
|
|
+ {
|
|
|
+ return (str == null ? "" : str.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取字符串
|
|
|
+ *
|
|
|
+ * @param str 字符串
|
|
|
+ * @param start 开始
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ public static String substring(final String str, int start)
|
|
|
+ {
|
|
|
+ if (str == null)
|
|
|
+ {
|
|
|
+ return NULLSTR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (start < 0)
|
|
|
+ {
|
|
|
+ start = str.length() + start;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (start < 0)
|
|
|
+ {
|
|
|
+ start = 0;
|
|
|
+ }
|
|
|
+ if (start > str.length())
|
|
|
+ {
|
|
|
+ return NULLSTR;
|
|
|
+ }
|
|
|
+
|
|
|
+ return str.substring(start);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取字符串
|
|
|
+ *
|
|
|
+ * @param str 字符串
|
|
|
+ * @param start 开始
|
|
|
+ * @param end 结束
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ public static String substring(final String str, int start, int end)
|
|
|
+ {
|
|
|
+ if (str == null)
|
|
|
+ {
|
|
|
+ return NULLSTR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (end < 0)
|
|
|
+ {
|
|
|
+ end = str.length() + end;
|
|
|
+ }
|
|
|
+ if (start < 0)
|
|
|
+ {
|
|
|
+ start = str.length() + start;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (end > str.length())
|
|
|
+ {
|
|
|
+ end = str.length();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (start > end)
|
|
|
+ {
|
|
|
+ return NULLSTR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (start < 0)
|
|
|
+ {
|
|
|
+ start = 0;
|
|
|
+ }
|
|
|
+ if (end < 0)
|
|
|
+ {
|
|
|
+ end = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return str.substring(start, end);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化文本, {} 表示占位符<br>
|
|
|
+ * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
|
|
|
+ * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
|
|
|
+ * 例:<br>
|
|
|
+ * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
|
|
|
+ * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
|
|
|
+ * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
|
|
|
+ *
|
|
|
+ * @param template 文本模板,被替换的部分用 {} 表示
|
|
|
+ * @param params 参数值
|
|
|
+ * @return 格式化后的文本
|
|
|
+
|
|
|
+ public static String format(String template, Object... params)
|
|
|
+ {
|
|
|
+ if (isEmpty(params) || isEmpty(template))
|
|
|
+ {
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+ return StrFormatter.format(template, params);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下划线转驼峰命名
|
|
|
+ */
|
|
|
+ public static String toUnderScoreCase(String s)
|
|
|
+ {
|
|
|
+ if (s == null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ boolean upperCase = false;
|
|
|
+ for (int i = 0; i < s.length(); i++)
|
|
|
+ {
|
|
|
+ char c = s.charAt(i);
|
|
|
+
|
|
|
+ boolean nextUpperCase = true;
|
|
|
+
|
|
|
+ if (i < (s.length() - 1))
|
|
|
+ {
|
|
|
+ nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((i > 0) && Character.isUpperCase(c))
|
|
|
+ {
|
|
|
+ if (!upperCase || !nextUpperCase)
|
|
|
+ {
|
|
|
+ sb.append(SEPARATOR_CHAR);
|
|
|
+ }
|
|
|
+ upperCase = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ upperCase = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ sb.append(Character.toLowerCase(c));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否包含字符串
|
|
|
+ *
|
|
|
+ * @param str 验证字符串
|
|
|
+ * @param strs 字符串组
|
|
|
+ * @return 包含返回true
|
|
|
+ */
|
|
|
+ public static boolean inStringIgnoreCase(String str, String... strs)
|
|
|
+ {
|
|
|
+ if (str != null && strs != null)
|
|
|
+ {
|
|
|
+ for (String s : strs)
|
|
|
+ {
|
|
|
+ if (str.equalsIgnoreCase(trim(s)))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转数组
|
|
|
+ * @param params 字符串
|
|
|
+ * @param regex 截取字符
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String [] splitStr(String params, String regex){
|
|
|
+ regex = StringUtils.isNotBlank(regex) ? regex : ",";
|
|
|
+ return StringUtils.isNotBlank(params) ? params.split(regex) : new String[]{};
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Object对象转字符串
|
|
|
+ * @param obj
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String objectStr(Object obj){
|
|
|
+ if (obj != null) {
|
|
|
+ return obj.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String trimNull(Object str) {
|
|
|
+ if (str == null) {
|
|
|
+ return "";
|
|
|
+ } else {
|
|
|
+ return str instanceof String ? ((String)str).trim() : str.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析出url请求的路径,包括页面
|
|
|
+ * @param strURL url地址
|
|
|
+ * @return url路径
|
|
|
+ */
|
|
|
+ public static String urlPage(String strURL) {
|
|
|
+ String strPage = null;
|
|
|
+ String[] arrSplit = null;
|
|
|
+ strURL = strURL.trim().toLowerCase();
|
|
|
+ arrSplit = strURL.split("[?]");
|
|
|
+ if (strURL.length() > 0) {
|
|
|
+ if (arrSplit.length > 1) {
|
|
|
+ if (arrSplit[0] != null) {
|
|
|
+ strPage = arrSplit[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 去掉url中的路径,留下请求参数部分
|
|
|
+ *
|
|
|
+ * @param strURL url地址
|
|
|
+ * @return url请求参数部分
|
|
|
+ */
|
|
|
+ private static String truncateUrlPage(String strURL) {
|
|
|
+ String strAllParam = null;
|
|
|
+ String[] arrSplit = null;
|
|
|
+ strURL = strURL.trim().toLowerCase();
|
|
|
+ arrSplit = strURL.split("[?]");
|
|
|
+ if (strURL.length() > 1) {
|
|
|
+ if (arrSplit.length > 1) {
|
|
|
+ if (arrSplit[1] != null) {
|
|
|
+ strAllParam = arrSplit[1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strAllParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析出url参数中的键值对
|
|
|
+ * 如 "index.jsp?Action=del&id=123",解析出Action:del,id:123存入map中
|
|
|
+ *
|
|
|
+ * @param url url地址
|
|
|
+ * @return url请求参数部分
|
|
|
+ */
|
|
|
+ public static Map<String, String> urlRequest(String url) {
|
|
|
+ Map<String, String> mapRequest = new HashMap<String, String>();
|
|
|
+ String[] arrSplit = null;
|
|
|
+ String strUrlParam = truncateUrlPage(url);
|
|
|
+ if (strUrlParam == null) {
|
|
|
+ return mapRequest;
|
|
|
+ }
|
|
|
+ //每个键值为一组 www.2cto.com
|
|
|
+ arrSplit = strUrlParam.split("[&]");
|
|
|
+ for (String strSplit : arrSplit) {
|
|
|
+ String[] arrSplitEqual = null;
|
|
|
+ arrSplitEqual = strSplit.split("[=]");
|
|
|
+ //解析出键值
|
|
|
+ if (arrSplitEqual.length > 1) {
|
|
|
+ //正确解析
|
|
|
+ mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
|
|
|
+ } else {
|
|
|
+ if (arrSplitEqual[0] != "") {
|
|
|
+ //只有参数没有值,不加入
|
|
|
+ mapRequest.put(arrSplitEqual[0], "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return mapRequest;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析出url参数中的键值对
|
|
|
+ * 如 "Action=del&id=123",解析出Action:del,id:123存入map中
|
|
|
+ *
|
|
|
+ * @param strUrlParam url地址
|
|
|
+ * @return url请求参数部分
|
|
|
+ */
|
|
|
+ public static Map<String, String> urlParams(String strUrlParam) {
|
|
|
+ Map<String, String> mapRequest = new HashMap<String, String>();
|
|
|
+ String[] arrSplit = null;
|
|
|
+ if (strUrlParam == null) {
|
|
|
+ return mapRequest;
|
|
|
+ }
|
|
|
+ //每个键值为一组 www.2cto.com
|
|
|
+ arrSplit = strUrlParam.split("[&]");
|
|
|
+ for (String strSplit : arrSplit) {
|
|
|
+ String[] arrSplitEqual = null;
|
|
|
+ arrSplitEqual = strSplit.split("[=]");
|
|
|
+ //解析出键值
|
|
|
+ if (arrSplitEqual.length > 1) {
|
|
|
+ //正确解析
|
|
|
+ mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
|
|
|
+ } else {
|
|
|
+ if (arrSplitEqual[0] != "") {
|
|
|
+ //只有参数没有值,不加入
|
|
|
+ mapRequest.put(arrSplitEqual[0], "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return mapRequest;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 替换掉HTML标签方法
|
|
|
+ */
|
|
|
+ public static String replaceHtml(String html) {
|
|
|
+ if (isBlank(html)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String regEx = "<.+?>";
|
|
|
+ Pattern p = Pattern.compile(regEx);
|
|
|
+ Matcher m = p.matcher(html);
|
|
|
+ String s = m.replaceAll("");
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 替换为手机识别的HTML,去掉样式及属性,保留回车。
|
|
|
+ *
|
|
|
+ * @param html
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String replaceMobileHtml(String html) {
|
|
|
+ if (html == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为Double类型
|
|
|
+ */
|
|
|
+ public static Double toDouble(Object val){
|
|
|
+ if (val == null){
|
|
|
+ return 0D;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Double.valueOf(trim(val.toString()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ return 0D;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为Float类型
|
|
|
+ */
|
|
|
+ public static Float toFloat(Object val) {
|
|
|
+ return toDouble(val).floatValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为Long类型
|
|
|
+ */
|
|
|
+ public static Long toLong(Object val){
|
|
|
+ return toDouble(val).longValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为Integer类型
|
|
|
+ */
|
|
|
+ public static Integer toInteger(Object val){
|
|
|
+ return toLong(val).intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Object转String
|
|
|
+ * @param val 源字符串
|
|
|
+ * @author dengyinghui
|
|
|
+ * @date 2018/2/27
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String valueOf(Object val){
|
|
|
+ if(val == null){
|
|
|
+ return "";
|
|
|
+ } else{
|
|
|
+ return String.valueOf(val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String toHex(byte[] bytes) {
|
|
|
+ final char[] hexDigits = "0123456789ABCDEF".toCharArray();
|
|
|
+ StringBuilder ret = new StringBuilder(bytes.length * 2);
|
|
|
+ for (int i=0; i<bytes.length; i++) {
|
|
|
+ ret.append(hexDigits[(bytes[i] >> 4) & 0x0f]);
|
|
|
+ ret.append(hexDigits[bytes[i] & 0x0f]);
|
|
|
+ }
|
|
|
+ return ret.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 替换字符串${xxxx}
|
|
|
+ * @param content 账号创建成功,欢迎使用危品汇!登录账号:${account},默认密码:${pwd}!
|
|
|
+ * @param regex 需要替换的字符(account)
|
|
|
+ * @param replacement 替换值(173xxxxxxxx)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String replace(String content, String regex, String replacement){
|
|
|
+ if (content == null || regex == null || replacement == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Matcher matcher = pattern.matcher(content);
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ while (matcher.find()) {
|
|
|
+ String group = matcher.group().replace("${", "").replace("}", "");
|
|
|
+ if (group.equals(regex)){
|
|
|
+ matcher.appendReplacement(strBuf, replacement);
|
|
|
+ return matcher.appendTail(strBuf).toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return matcher.appendTail(strBuf).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 替换字符串${xxxx}
|
|
|
+ * @param content 账号创建成功,欢迎使用危品汇!登录账号:${account},默认密码:${pwd}!
|
|
|
+ * @param params {"account":"xxxxx", "pwd":"xxxx"}
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String replace1(String content, Map<String, Object> params){
|
|
|
+ if (content == null || params == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Matcher matcher = pattern.matcher(content);
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ while(matcher.find()){
|
|
|
+ String group = matcher.group().replace("${", "").replace("}", "");
|
|
|
+ if (params.get(group) != null){
|
|
|
+ String replacement = StringUtils.objectStr(params.get(group));
|
|
|
+ matcher.appendReplacement(strBuf, replacement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ matcher.appendTail(strBuf);
|
|
|
+ return strBuf.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String capitalize(final String str) {
|
|
|
+ int strLen;
|
|
|
+ if (str == null || (strLen = str.length()) == 0) {
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ final int firstCodepoint = str.codePointAt(0);
|
|
|
+ final int newCodePoint = Character.toTitleCase(firstCodepoint);
|
|
|
+ if (firstCodepoint == newCodePoint) {
|
|
|
+ // already capitalized
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ final int newCodePoints[] = new int[strLen]; // cannot be longer than the char array
|
|
|
+ int outOffset = 0;
|
|
|
+ newCodePoints[outOffset++] = newCodePoint; // copy the first codepoint
|
|
|
+ for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
|
|
|
+ final int codepoint = str.codePointAt(inOffset);
|
|
|
+ newCodePoints[outOffset++] = codepoint; // copy the remaining ones
|
|
|
+ inOffset += Character.charCount(codepoint);
|
|
|
+ }
|
|
|
+ return new String(newCodePoints, 0, outOffset);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args){
|
|
|
+ Map<String, Object> param = new HashMap();
|
|
|
+ param.put("account", "17358629955");
|
|
|
+ param.put("pwd", "123456");
|
|
|
+ String template = "账号创建成功,欢迎使用危品汇!登录账号:${account},默认密码:${pwd}!";
|
|
|
+ for(String key : param.keySet()){
|
|
|
+ template = StringUtils.replace(template, key, StringUtils.objectStr(param.get(key)));
|
|
|
+ }
|
|
|
+ System.out.println(template);
|
|
|
+
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ m.put("account", "han");
|
|
|
+ m.put("pwd", "zhong");
|
|
|
+ System.out.println( replace1(template, m));
|
|
|
+ }
|
|
|
+}
|