标签:cto support rac dha seq object hash instance try
package com.intfish.util; /** * */ import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.text.StrBuilder; import java.io.UnsupportedEncodingException; import java.text.NumberFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class StringUtils extends org.apache.commons.lang3.StringUtils { /** * 替换掉HTML标签方法 */ public static String stripHtml(String html) { if (isBlank(html)){ return ""; } String regEx = "<.+?>"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(html); String s = m.replaceAll(""); return s; } /** * 缩略字符串(不区分中英文字符) * @param str 目标字符串 * @param length 截取长度 * @return */ public static String abbr(String str, int length) { if (str == null) { return ""; } try { StringBuilder sb = new StringBuilder(); int currentLength = 0; for (char c : stripHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) { currentLength += String.valueOf(c).getBytes("GBK").length; if (currentLength <= length - 3) { sb.append(c); } else { sb.append("..."); break; } } return sb.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ""; } /**空字符串*/ private static final String NULLSTR = ""; /** * 获取参数不为空 * @param value * @param defaultValue * @param <T> * @return */ public static <T> T nvl(T value,T defaultValue){ return value !=null ? value : defaultValue; } /** * 判断一个对象是否为空 * @param object * @return true为空 false为非空 */ public static boolean isNull(Object object){ return object == null; } /** * 判断一个对象为非空 * @param object * @return true:空 false:非空 */ public static boolean isNotNUll(Object object){ return !isNull(object); } /** * 判断一个Collection是否为空,包含List,Set,Queue * @param coll * @return true:为空,false:非空 */ public static boolean isEmpty(Collection<?> coll){ return isNull(coll) || coll.isEmpty(); } /** * 判断一个Collection是否非空 * @param coll * @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 * @return rue:为空,false:非空 */ public static boolean isEmpty(Map<?,?> map){ return isNull(map) || map.isEmpty(); } /** * 判断一个Map是否非空 * @param map * @return true:非空 false:空 */ public static boolean isNotEmpty(Map<?,?> map){ return !isEmpty(map); } /** * 判断一个字符串是否为空 * @param str * @return true:非空 false:空 */ public static boolean isEmpty(String str){ return isNull(str) || NULLSTR.equals(str.trim()); } /** * 判断一个字符串为非空 * @param str * @return true:非空 false:空 */ public static boolean isNotEmpty(String str){ return !isEmpty(str); } public static boolean isEmpty(Object obj){ return obj == null || "".equals(obj); } public static boolean isNotEmpty(Object obj){ return !isEmpty(obj); } /** * 判断一个对象是否是数组类型 * @param object * @return true:是数组 false:不是数组 */ // public static boolean isArray(Object object){ // return isNotNUll(object) && object.getClass().isArray(); // } /** * 去字符串头尾空格 * @param str * @return */ public static String trim(String str){ return str == null ? "" : str.trim(); } /** * 截取字符串 * @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); } /** * 字符串首字母小写 * @param str * @return */ public static String uncapitalize(String str){ int strLen; if (str == null || (strLen = str.length()) == 0){ return str; } return new StrBuilder(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString(); } /** * 判断是否包含字符串 * @param str * @param strs * @return */ 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; } /** * 将带下划线的字符串转换为驼峰形式 如 hello_world -> HelloWorld,HELLO_WORLD -> HelloWorld * @param name * @return */ public static String convertToCamelCase(String name){ StringBuilder result = new StringBuilder(); //如果字符串为空,没必要转换 if (name == null || isEmpty(name)){ return ""; //如果字符串不包含下划线,仅首字母大写 }else if (!name.contains("_")){ return name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase(); } //使用“_”分割字符串 String[] camels = name.split("_"); for (String camel : camels){ //跳过字符串开头中间结尾的下划线,以及双下划线 if (camel.isEmpty()){ continue; } //首字母大写 result.append(camel.substring(0,1).toUpperCase()); result.append(camel.substring(1).toLowerCase()); } return result.toString(); } /** * 字符串数据处理 * @param value * @return */ public static String valueAsStr(Object value){ if (value instanceof String){ return (String) value; } if (value != null){ return value.toString(); }else { return null; } } /** * 整型数据处理 */ public static Integer valueAsInt(Object value){ if (value instanceof Integer){ return (Integer) value; }else if (value instanceof Number){ return ((Number)value).intValue(); }else if (value instanceof String){ if ("NaN".equals(value)){ return null; } return Integer.valueOf((String) value); }else if (value instanceof Boolean){ return ((Boolean)value) ? 1 : 0; } else { return null; } } public static String getStringJoinedValue(CharSequence delimiter, Iterable<? extends CharSequence> elements){ String str = ""; if (null != elements ){ str += String.join(delimiter,elements); } return str; } public static List<String> doubleListToStringList(List<Double> data){ if(data!=null && data.size()>0){ NumberFormat nf = NumberFormat.getInstance(); nf.setGroupingUsed(false); return data.stream().map(nf::format).collect(Collectors.toList()); } return new ArrayList<>(); } /** * 将key,value按splitChar分割封装成map * @param key "channelid,islogin" * @param value "vivo,yes" * @param splitChar 分割符 * @author wangxiaolei * @date 2020/4/15 19:15 * @return java.util.Map<java.lang.String,java.lang.Object> {"channelid":"vivo", "islogin","yes"} */ public static Map<String,Object> splitToMap(String key, String value, String splitChar){ Map<String, Object> resultMap = new LinkedHashMap<>(); if(!StringUtils.isEmpty(key)){ String[] k = key.split(splitChar); String[] v = value.split(splitChar); for (int i = 0; i < k.length; i++) { resultMap.put(k[i], v[i]); } } return resultMap; } /** * 将 字符串 value 根据splitChar为分割符,在第n个位置切成为两段。如 n=1, "a,b,c,d" => ["a","b,c,d"] * @param value 字符串 * @param n 前n个 * @param splitChar 分割符 * @author wangxiaolei * @date 2020/4/15 20:50 * @return java.util.List<java.lang.String> 返回长度为2的list */ public static List<String> splitTopN(String value, int n, String splitChar){ List<String> result = new ArrayList<>(); if(!StringUtils.isEmpty(value)){ String[] vs = value.split(splitChar); List<String> a = new ArrayList<>(); List<String> b = new ArrayList<>(); int min = Math.min(vs.length, n); int i = 0; for(i = 0;i < min; i++){ a.add(vs[i]); } for(;i < vs.length; i++){ b.add(vs[i]); } result.add(String.join(splitChar,a)); result.add(String.join(splitChar,b)); } while(result.size() < 2){ result.add(""); } return result; } /** * 将map的字段合并成string类型 {"a":1},{"b",2} ==> a:1,b:2 * @param list * @return */ public static String mapToFieldValue(List<Map<String, Object>> list){ StringBuilder sb = new StringBuilder(); if(!StringUtils.isEmpty(list)){ for (Map<String, Object> stringObjectMap : list) { for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); sb.append(key).append(":").append(value).append(","); } } } String str = sb.toString(); if(str.endsWith(",")) { return str.substring(0, str.length() - 1); //去掉最后面的 "," } return str; } }
标签:cto support rac dha seq object hash instance try
原文地址:https://www.cnblogs.com/f-society/p/12852578.html