package com.common.core.utils;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author : zhouxb
|
* date: 2017-10-25 23:54
|
*/
|
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|
/**
|
* SQL格式化表名和字段名加"`"
|
*
|
* @param fieldName
|
* @return
|
*/
|
public static String formatFieldNameColumn(String fieldName) {
|
return new StringBuffer("`").append(fieldName).append("`").toString();
|
}
|
|
public static String formatStringFiledVal(String fieldName) {
|
return new StringBuffer("'").append(fieldName).append("'").toString();
|
}
|
|
public static String formatFieldNameColumn4Rdb(String fieldName) {
|
return new StringBuffer("\"").append(fieldName).append("\"").toString();
|
}
|
|
/**
|
* 去掉字符串指定前缀部分
|
*
|
* @param source 字符串
|
* @param prefix 前缀
|
*/
|
public static String cutPrefix(String source, String prefix) {
|
if (isNotBlank(source)) {
|
if (isNotEmpty(prefix)) {
|
if (source.startsWith(prefix)) {
|
int prefixLen = prefix.length();
|
source = source.substring(prefixLen);
|
}
|
}
|
}
|
return source;
|
}
|
|
/**
|
* 去掉字符串指定后缀部分
|
*
|
* @param source 字符串
|
* @param suffix 后缀
|
*/
|
public static String cutSuffix(String source, String suffix) {
|
if (isNotBlank(source)) {
|
if (isNotEmpty(suffix)) {
|
int suffixLen = suffix.length();
|
source = source.substring(0, source.length() - suffixLen);
|
}
|
}
|
return source;
|
}
|
|
/**
|
* 去掉字符串中指定字符及其后面部分
|
*
|
* @param source 字符串
|
* @param separator 指定字符
|
*/
|
public static String cutLaterOfCharacter(String source, String separator) {
|
if (isNotBlank(source)) {
|
if (isNotEmpty(separator)) {
|
int index = source.indexOf(separator);
|
if (index > -1) {
|
source = source.substring(0, index);
|
}
|
}
|
}
|
return source;
|
}
|
|
/**
|
* sql语句like部分
|
* 增加对%转义
|
*
|
* @param in
|
* @return
|
*/
|
public static String sqlLike(String in) {
|
// if (StringUtils.isNotEmpty(in) && in.contains("%")) {
|
// in = in.replace("\\", "\\\\");
|
// in = in.replace("%", "\\%");
|
// in = in.replace("_", "\\_");
|
// }
|
return "%" + sqlLikeXML(in) + "%";
|
}
|
|
public static String sqlLikeXML(String in) {
|
if (StringUtils.isNotEmpty(in) && (in.contains("%") || in.contains("_") || in.contains("\\"))) {
|
in = in.replace("\\", "\\\\");
|
in = in.replace("%", "\\%");
|
in = in.replace("_", "\\_");
|
}
|
return in;
|
}
|
|
/**
|
* 按指定分割符分割字符串,返回结果中不包含空字符串
|
*/
|
public static String[] tokenizeToStringArray(String cs, String delimiters) {
|
String[] csAry = org.springframework.util.StringUtils.tokenizeToStringArray(cs, delimiters);
|
List<String> result = new ArrayList<>(csAry.length);
|
for (String csa : csAry) {
|
if (isNotBlank(csa)) {
|
result.add(csa);
|
}
|
}
|
return result.toArray(new String[result.size()]);
|
}
|
|
/**
|
* 拆分token方法
|
*
|
* @param cs
|
* @return
|
*/
|
public static String[] tokenizeToStringArray(String cs) {
|
return tokenizeToStringArray(cs, ",");
|
}
|
|
/**
|
* 将集合用转换为指定分隔符连接的字符串
|
*
|
* @param coll
|
* @param delimiter
|
* @return
|
*/
|
public static String collectionToDelimitedString(Collection<?> coll, String delimiter) {
|
return org.springframework.util.StringUtils.collectionToDelimitedString(coll, delimiter);
|
}
|
|
/**
|
* 将集合用","连接的字符串
|
*
|
* @param coll
|
* @return
|
*/
|
public static String collectionToDelimitedString(Collection<?> coll) {
|
return collectionToDelimitedString(coll, ",");
|
}
|
|
/**
|
* 将数组转换为用","连接的字符串
|
*
|
* @param ary
|
* @return
|
*/
|
// public static String arrayToCommaDelimitedString(Object[] ary) {
|
// return arrayToDelimitedString(ary, ",");
|
// }
|
//
|
// /**
|
// * 将数组转换为用指定分隔符连接的字符串
|
// *
|
// * @param ary
|
// * @param delimiter
|
// * @return
|
// */
|
// public static String arrayToDelimitedString(Object[] ary, String delimiter) {
|
// if (ObjectUtils.isEmpty(ary)) {
|
// return "";
|
// }
|
// if (ary.length == 1) {
|
// return ObjectUtils.nullSafeToString(ary[0]);
|
// }
|
//
|
// StringBuilder sb = new StringBuilder();
|
// for (int i = 0; i < ary.length; i++) {
|
// if (i > 0) {
|
// sb.append(delimiter);
|
// }
|
// sb.append(ary[i]);
|
// }
|
// return sb.toString();
|
// }
|
|
/**
|
* 根据字段类型返回字段作为where条件查询判断字段时使用的分隔符
|
*
|
* @param type
|
* @return
|
*/
|
public static String getSeparator(String type) {
|
if ("string".equalsIgnoreCase(type)) {
|
return "'";
|
} else {
|
return "";
|
}
|
}
|
|
/**
|
* 返回隐藏信息的String
|
*
|
* @param input
|
* @return
|
*/
|
public static String getConcealStr(String input) {
|
String ss = "*";
|
String ds = "***";
|
if (StringUtils.isNotBlank(input)) {
|
if (input.length() <= 2) {
|
int i = input.length() / 2;
|
if (i == 0) {
|
return ss;
|
} else {
|
return input.substring(0, i) + ss;
|
}
|
} else {
|
int i = input.length() / 3;
|
return input.substring(0, i) + ds + input.substring(input.length() - i);
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 数据脱敏方法
|
*
|
* @param input
|
* @return
|
*/
|
public static String concealString(String input) {
|
if (StringUtils.isEmpty(input)) {
|
return input;
|
}
|
|
return StringUtils.collectionToDelimitedString(Arrays.stream(input.split(",")).map(e -> concealStringSingle(e)).collect(Collectors.toList()));
|
|
}
|
|
/**
|
* 单个字符串数据脱敏方法
|
*
|
* @param input
|
* @return
|
*/
|
public static String concealStringSingle(String input) {
|
if (input.startsWith("Untagged") || input.startsWith("Other tag")) {
|
return input;
|
}
|
if (1 == input.length()) {
|
return "*";
|
}
|
|
int i = (int) Math.ceil((double) input.length() / 3);
|
|
int j = (int) Math.floor((double) input.length() / 3);
|
StringBuffer sbf = new StringBuffer(input.substring(0, i));
|
for (int x = 0; x < input.length() - i - j; x++) {
|
sbf.append("*");
|
}
|
|
return sbf.append(input.substring(input.length() - j)).toString();
|
}
|
|
|
// public static String formatList2SqlInString(List<String> propertyList) {
|
// if (CollectionUtils.isNotEmpty(propertyList)) {
|
// return propertyList.stream().map(e -> new StringBuffer().append("'").append(e).append("'").toString()).collect(Collectors.joining(CommonSqlConst.F_COMMA));
|
// }
|
// return null;
|
// }
|
|
// public static String formatSqlFieldNotBlank(String fieldName) {
|
// if (StringUtils.isNotBlank(fieldName)) {
|
// return new StringBuffer().append(fieldName).append(" is not null ").append(CommonSqlConst.SQL_AND).append(fieldName).append(" !='' ").toString();
|
// }
|
// return null;
|
// }
|
|
public static String formatSqlCondition(String sql) {
|
sql = sql.replace("&&", "and").replace("||", "or").replace("`", "");
|
|
return sql;
|
}
|
|
public static String formatByMap(String content, Map<String, String> varValueMap) {
|
for(Map.Entry<String,String> entry:varValueMap.entrySet()){
|
content=content.replace("{"+entry.getKey()+"}",entry.getValue());
|
}
|
return content;
|
}
|
}
|