old mode 100644
new mode 100755
| | |
| | | package com.java110.utils.util; |
| | | |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Date; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | |
| | |
| | | */ |
| | | |
| | | public class StringUtil { |
| | | |
| | | |
| | | /** |
| | | * Description: 格式化字符串(用户组建表使用) <br> |
| | | * |
| | |
| | | } |
| | | } |
| | | |
| | | public static boolean isInteger(String str) { |
| | | try { |
| | | double value = Double.parseDouble(str); |
| | | if (value == Math.ceil(value)) { |
| | | return true; |
| | | } |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * Description: 获得字符(byte)的实际长度<br> |
| | | * 1、…<br> |
| | |
| | | * @return |
| | | */ |
| | | public static boolean isEmpty(String str) { |
| | | return str == null || "".equals(str); |
| | | return str == null || "".equals(str.trim()); |
| | | } |
| | | |
| | | /** |
| | | * 校验是否为JSON |
| | | * |
| | | * @param msg |
| | | * @return |
| | | */ |
| | | public static Boolean isJsonObject(String msg) { |
| | | try { |
| | | JSONObject.parseObject(msg); |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | return description; |
| | | } |
| | | |
| | | /** |
| | | * json是否包含key 并且存在值 |
| | | * |
| | | * @param param |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static boolean jsonHasKayAndValue(JSONObject param, String key) { |
| | | if (param == null) { |
| | | return false; |
| | | } |
| | | |
| | | if (!param.containsKey(key)) { |
| | | return false; |
| | | } |
| | | |
| | | if (isEmpty(param.getString(key))) { |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * json是否包含key 并且存在值 |
| | | * |
| | | * @param param |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static boolean jsonHasKayAndValue(String param, String key) { |
| | | JSONObject paramObj = null; |
| | | try { |
| | | paramObj = JSONObject.parseObject(param); |
| | | return jsonHasKayAndValue(paramObj, key); |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | public static String delHtmlTag(String str) { |
| | | String newstr = ""; |
| | | newstr = str.replaceAll("<[.[^>]]*>", ""); |
| | | newstr = newstr.replaceAll(" ", ""); |
| | | newstr = newstr.replaceAll(" ", ""); |
| | | return newstr; |
| | | } |
| | | |
| | | |
| | | public static String encodeEmoji(String orgStr) { |
| | | if (StringUtil.isEmpty(orgStr)) { |
| | | return orgStr; |
| | | } |
| | | String temp = orgStr; |
| | | Pattern pattern = Pattern.compile("[^\u0000-\uffff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE); |
| | | Matcher matcher = pattern.matcher(orgStr); |
| | | while (matcher.find()) { |
| | | StringBuilder sb = new StringBuilder("[em:"); |
| | | String mStr = matcher.group(); |
| | | for (int i = 0; i < mStr.length(); i++) { |
| | | int emoji = mStr.charAt(i); |
| | | if (i < mStr.length() - 1) { |
| | | sb.append(emoji).append('-'); |
| | | } else { |
| | | sb.append(emoji).append(']'); |
| | | } |
| | | } |
| | | temp = temp.replaceAll(mStr, sb.toString()); |
| | | } |
| | | return temp; |
| | | } |
| | | |
| | | public static String decodeEmoji(String orgStr) { |
| | | if (StringUtil.isEmpty(orgStr)) { |
| | | return orgStr; |
| | | } |
| | | String temp = orgStr; |
| | | Pattern pattern = Pattern.compile("\\[em:[\\d\\-]+\\]", Pattern.CASE_INSENSITIVE); |
| | | Matcher matcher = pattern.matcher(orgStr); |
| | | while (matcher.find()) { |
| | | StringBuilder sb = new StringBuilder("\\[em:"); |
| | | StringBuilder emojiSb = new StringBuilder(); |
| | | String mStr = matcher.group(); |
| | | String[] emojis = mStr.substring(4, mStr.length() - 1).split("-"); |
| | | for (int i = 0; i < emojis.length; i++) { |
| | | int emoji = Integer.parseInt(emojis[i]); |
| | | emojiSb.append((char) emoji); |
| | | if (i < emojis.length - 1) { |
| | | sb.append(emoji).append("\\-"); |
| | | } else { |
| | | sb.append(emoji).append("\\]"); |
| | | } |
| | | } |
| | | temp = temp.replaceAll(sb.toString(), emojiSb.toString()); |
| | | } |
| | | return temp; |
| | | } |
| | | |
| | | /** |
| | | * 下划线转驼峰 |
| | | */ |
| | | public static JSONObject lineToHump(JSONObject json) { |
| | | Pattern linePattern = Pattern.compile("_(\\w)"); |
| | | JSONObject newJson = new JSONObject(); |
| | | String value = ""; |
| | | for (String str : json.keySet()) { |
| | | str = str.toLowerCase(); |
| | | Matcher matcher = linePattern.matcher(str); |
| | | StringBuffer sb = new StringBuffer(); |
| | | while (matcher.find()) { |
| | | matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); |
| | | } |
| | | matcher.appendTail(sb); |
| | | value = json.getString(str); |
| | | if (StringUtil.isEmpty(value)) { |
| | | newJson.put(sb.toString(), json.getString(str)); |
| | | newJson.put(str, json.getString(str)); |
| | | continue; |
| | | } |
| | | if (value.startsWith("'")) { |
| | | value = value.replace("'", ""); |
| | | } |
| | | |
| | | if (value.endsWith("'")) { |
| | | value = value.substring(0, value.length() - 1); |
| | | } |
| | | |
| | | newJson.put(sb.toString(), value); |
| | | newJson.put(str, value); |
| | | |
| | | } |
| | | |
| | | return newJson; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String value = "'123'"; |
| | | if (value.startsWith("'")) { |
| | | value = value.replace("'", ""); |
| | | } |
| | | |
| | | if (value.endsWith("'")) { |
| | | value = value.substring(0, value.length() - 1); |
| | | } |
| | | System.out.printf(value); |
| | | } |
| | | |
| | | /** |
| | | * 下划线转驼峰 |
| | | */ |
| | | public static String lineToHump(String str) { |
| | | Pattern linePattern = Pattern.compile("_(\\w)"); |
| | | str = str.toLowerCase(); |
| | | Matcher matcher = linePattern.matcher(str); |
| | | StringBuffer sb = new StringBuffer(); |
| | | while (matcher.find()) { |
| | | matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); |
| | | } |
| | | matcher.appendTail(sb); |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 对人名进行脱敏处理,只显示姓和名字的最后一个字 |
| | | * |
| | | * @param name 原始人名 |
| | | * @return 脱敏后人名的字符串 |
| | | */ |
| | | public static String maskName(String name) { |
| | | if (name == null || name.isEmpty()) { |
| | | return name; |
| | | } |
| | | String[] parts = name.split(""); |
| | | |
| | | if(parts.length <= 2){ |
| | | return parts[0]+"*"; |
| | | } |
| | | |
| | | return parts[0]+"*" + parts[parts.length-1]; |
| | | } |
| | | |
| | | } |