aiccs-api/src/main/java/com/chinaweal/aiccs/common/util/StringUtils.java

325 lines
9.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.chinaweal.aiccs.common.util;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
/**
* 自动以字符串工具类继承于org.apache.commons.lang3.StringUtils
*
* @author <a href="https://blog.lroyia.top">lroyia</a>
* @since 2020/7/31 14:25
**/
public abstract class StringUtils extends org.apache.commons.lang3.StringUtils {
/**
* 将Object转为字符串null时返回空字串
*
* @param obj obj
* @return 字符串
* @author lroyia
* @since 2020年7月31日 14:28:22
*/
public static String tranObject(Object obj) {
return obj == null ? EMPTY : obj.toString();
}
/**
* 判断字符是否不相等
*
* @param cs1 字符1
* @param cs2 字符2
* @return 判断结果
* @author lroyia
* @since 2025年2月28日11:01:30
*/
public static boolean notEquals(final CharSequence cs1, final CharSequence cs2) {
return !equals(cs1, cs2);
}
/**
* 获取文件扩展名
*
* @param fileName 文件全名
* @return 文件扩展名
* @author lroyia
* @since 2020年9月23日 14:37:45
*/
public static String getFileExt(String fileName) {
int pointIdx = fileName.lastIndexOf(".");
if (pointIdx < 0) return EMPTY;
return fileName.substring(pointIdx);
}
/**
* 是否有统一码
*
* @param uscc 统一社会信用底阿妈
* @return boolean
* @author lroyia
* @since 2021年1月22日 11:15:48
*/
public static boolean isUscc(String uscc) {
if (isNotBlank(uscc)) {
return trimToEmpty(uscc).length() == 18;
}
return false;
}
/**
* 是否等于这些字符串中的其中一个
*
* @param source 源字符串
* @param inStr 比较的字符串信息
* @return 比较结果
* @author lroyia
* @since 2021年3月23日 15:56:46
*/
public static boolean in(String source, String... inStr) {
if (inStr == null) return false;
for (String each : inStr) {
if (equals(source, each)) return true;
}
return false;
}
/**
* 是否不等于这些字符串中的任何一个
*
* @param source 源字符串
* @param notInStr 比较字符串
* @return 比较结果
* @author lroyia
* @since 2021年3月23日 15:59:07
*/
public static boolean notIn(String source, String... notInStr) {
return !in(source, notInStr);
}
/**
* 从字串中提取辖区信息
*
* @param str 提取的字符串
* @return 提取结果
* @author lroyia
* @since 2021年6月21日 16:58:40
*/
public static String getRegionByStr(String str) {
// for (String s : Constant.GZ_All_REGION) {
// if (str.contains(s.replace("区", EMPTY))) {
// return s;
// }
// }
return null;
}
// /**
// * 从字串中提取辖区码值
// *
// * @param str 提取的字符串
// * @return 提取结果
// * @author Lee
// * @since 2021年6月29日 17点38分
// */
// public static String getRegionCodeByStr(String str) {
// String area = "";
// if ("广州市市场监督管理局".equals(str)) {
// return "440100";
// } else {
// for (String s : Constant.GZ_All_REGION) {
// if (str.contains(s.replace("区", EMPTY))) {
// area = s;
// break;
// }
// }
// }
// if (StringUtils.isNotBlank(area)) {
// Set<String> keySet = Constant.NM_LOC_REGION_CODE.keySet();
// for (String code : keySet) {
// if (Constant.NM_LOC_REGION_CODE.get(code).contains(area)) {
// return code;
// }
// }
// }
// return null;
// }
/**
* 转换rest_log日志表的query值
*/
public static Map<String, String> changeRestLogQuery(String text) {
//不存在的key可以补上
String[] keys = {"nextNodeID", "handledate", "opinion", "remark", "nextPerformerids", "bizseq",
"workflowid", "tasklistid", "handler", "handlerid", "opiniontype", "opinioncontent",
"lostCreditExplainrem", "remRemark", "operOpinion", "remreasons", "isAccept",
"isAgreeRepair", "remexcpres", "applicant"};
Map<String, String> map = new LinkedHashMap<>();
for (String key : keys) {
int i = text.indexOf(key + ":");
if (i == -1) {
continue;
}
String substring = text.substring(i);
int end = substring.indexOf(" ");
if (end == -1) {
map.put(key, substring.substring((key + ":").length()));
} else {
String str = substring.substring((key + ":").length(), end);
map.put(key, str);
}
}
return map;
}
public static void main(String[] args) {
String text = "nextNodeID:endExptlist handledate:2021-11-19 11:30:16 opinion:同意 remark: isagree:1 nextPerformerids: bizseq:fddad9ec-7241-4f76-87c0-00fd33e4b274 workflowid:00b1ab71-a2a0-4cb5-b413-ac993e7ee25d tasklistid:0c4ece439e0c4eddcbe135f0216f85bc handler:黎树忠 handlerid:1cce40ea-ae8a-4918-915d-aa9e285155fa opiniontype:2 opinioncontent:同意";
Map<String, String> stringStringMap = changeRestLogQuery(text);
System.out.println(stringStringMap.get("bizseq"));
}
/**
* bytes数组转16进制字串
*
* @param bytes byte数组
* @return 十六进制字串
* @author lroyia
* @since 2021年11月27日 16:52:39
*/
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() < 2) {
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
/**
* 截取区编码前缀 如440000 -> 44
*
* @param region 区编码
* @return 截取结果
* @author lroyia
* @since 2021年12月29日 15:07:43
*/
public static String getRegionPrefixStr(String region) {
char[] chars = region.toCharArray();
int i = chars.length - 1;
for (; i > -1; i--) {
if (chars[i] != '0') {
if (i % 2 == 0) {
i++;
}
break;
}
}
return region.substring(0, i + 1);
}
/**
* 获取一个UUID
*
* @return uuid
* @author lroyia
* @since 2025年2月28日10:55:53
*/
public static String getUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 清除行政区划尾端的0
*
* @param regionCode 行政区划
* @return 清除后的0
* @author lroyia
* @since 2023年7月3日 14:02:52
*/
public static String clearRegionZero(String regionCode) {
if (isBlank(regionCode)) {
return EMPTY;
}
int maxLength = regionCode.length() / 2 > 3 ? 4 : (regionCode.length() / 2);
for (int i = maxLength; i > 0; i--) {
int startIndex = (i - 1) * 2;
int endIndex = i == 4 ? 9 : (i * 2);
if (clearSuffixZero(regionCode.substring(startIndex, endIndex)).isEmpty()) {
continue;
}
return regionCode.substring(0, endIndex);
}
return EMPTY;
}
/**
* 清除字符串尾部的0
*
* @param str 字符串
* @return 清除结果
* @author lroyia
* @since 2023年3月1日 09:22:43
*/
public static String clearSuffixZero(String str) {
if (isBlank(str)) {
return str;
}
int index = -1;
for (int i = str.length() - 1; i > -1; i--) {
if (str.charAt(i) != '0') {
break;
}
index = i;
}
if (index > -1) {
str = str.substring(0, index);
}
return str;
}
/**
* 后部补'0',将字串填充到目标长度
*
* @param str 原字串
* @param length 目标长度
* @return 填充结果
* @author lroyia
* @since 2023年6月1日 10:29:37
*/
public static String fillZeroSuffix(String str, int length) {
StringBuilder sb = new StringBuilder(str);
while (sb.length() < length) {
sb.append('0');
}
return sb.toString();
}
/**
* 前部补'0',将字串填充到目标长度
*
* @param str 原字串
* @param length 目标长度
* @return 填充结果
* @author lroyia
* @since 2023年6月1日 10:29:37
*/
public static String fillZeroPrefix(String str, int length) {
StringBuilder sb = new StringBuilder(str);
while (sb.length() < length) {
sb.insert(0, '0');
}
return sb.toString();
}
/**
* 获取一个新的UUID无横杠
* @return UUID
*/
public static String newUUID(){
return UUID.randomUUID().toString().replace("-", EMPTY);
}
}