package cn.lili.common.utils;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import java.beans.BeanInfo;
|
import java.beans.Introspector;
|
import java.beans.PropertyDescriptor;
|
import java.lang.reflect.Method;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.Random;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 字串工具类
|
*
|
* @author pikachu
|
*/
|
public class StringUtils extends StrUtil {
|
|
/**
|
* MD5加密方法
|
*
|
* @param str String
|
* @return String
|
*/
|
public static String md5(String str) {
|
MessageDigest messageDigest = null;
|
try {
|
messageDigest = MessageDigest.getInstance("MD5");
|
} catch (NoSuchAlgorithmException ex) {
|
ex.printStackTrace();
|
return null;
|
}
|
byte[] resultByte = messageDigest.digest(str.getBytes());
|
StringBuffer result = new StringBuffer();
|
for (int i = 0; i < resultByte.length; ++i) {
|
int v = 0xFF & resultByte[i];
|
if (v < 16) {
|
result.append("0");
|
}
|
result.append(Integer.toHexString(v));
|
}
|
return result.toString();
|
}
|
|
/**
|
* 获取随机数
|
*
|
* @param n 随机次数
|
* @return
|
*/
|
public static String getRandStr(int n) {
|
Random random = new Random();
|
String sRand = "";
|
for (int i = 0; i < n; i++) {
|
String rand = String.valueOf(random.nextInt(10));
|
sRand += rand;
|
}
|
return sRand;
|
}
|
|
/**
|
* 切个字符串,如果超出长度则切割
|
*
|
* @param var
|
* @param size
|
* @return
|
*/
|
public static String subStringLength(String var, Integer size) {
|
if (var.length() > size) {
|
return var.substring(0, (size - 4)).toString() + "...";
|
}
|
return var;
|
}
|
|
/**
|
* 对象转map
|
*
|
* @param obj
|
* @return
|
* @throws Exception
|
*/
|
public static Map<String, Object> objectToMap(Object obj) throws Exception {
|
if (obj == null) {
|
return null;
|
}
|
Map<String, Object> map = new HashMap<String, Object>(16);
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
|
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
for (PropertyDescriptor property : propertyDescriptors) {
|
String key = property.getName();
|
if (key.compareToIgnoreCase("class") == 0) {
|
continue;
|
}
|
Method getter = property.getReadMethod();
|
Object value = getter != null ? getter.invoke(obj) : null;
|
map.put(key, value);
|
}
|
|
return map;
|
}
|
|
|
/**
|
* 驼峰法转下划线
|
*/
|
public static String camel2Underline(String str) {
|
|
if (StrUtil.isBlank(str)) {
|
return "";
|
}
|
if (str.length() == 1) {
|
return str.toLowerCase();
|
}
|
StringBuffer sb = new StringBuffer();
|
for (int i = 1; i < str.length(); i++) {
|
if (Character.isUpperCase(str.charAt(i))) {
|
sb.append("_" + Character.toLowerCase(str.charAt(i)));
|
} else {
|
sb.append(str.charAt(i));
|
}
|
}
|
return (str.charAt(0) + sb.toString()).toLowerCase();
|
}
|
|
/**
|
* 如果给定字符串{@code str}中不包含{@code appendStr},则在{@code str}后追加{@code appendStr};
|
* 如果已包含{@code appendStr},则在{@code str}后追加{@code otherwise}
|
*
|
* @param str 给定的字符串
|
* @param appendStr 需要追加的内容
|
* @param otherwise 当{@code appendStr}不满足时追加到{@code str}后的内容
|
* @return 追加后的字符串
|
*/
|
public static String appendIfNotContain(String str, String appendStr, String otherwise) {
|
if (isEmpty(str) || isEmpty(appendStr)) {
|
return str;
|
}
|
if (str.contains(appendStr)) {
|
return str.concat(otherwise);
|
}
|
return str.concat(appendStr);
|
}
|
|
/**
|
* 切割字符串
|
*
|
* @param str 字符串
|
* @param length 长度
|
* @return 处理后的字符串
|
*/
|
public static String sub(String str, Integer length) {
|
if (str.length() < length) {
|
return str;
|
}
|
return str.substring(0, length);
|
}
|
|
/**
|
* 过滤特殊字符串
|
*
|
* @param str
|
* @return
|
*/
|
public static String filterSpecialChart(String str) {
|
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
|
Pattern p = Pattern.compile(regEx);
|
Matcher m = p.matcher(str);
|
return m.replaceAll("").trim();
|
}
|
|
/**
|
* double 转价格字符串
|
*
|
* @return
|
*/
|
public static String toFen(Double doubleValue) {
|
String str = doubleValue.toString();
|
|
if (!str.contains(".")) {
|
str = str + ".00";
|
} else if (str.substring(str.indexOf(".")).length() == 2) {
|
str = str + "0";
|
}
|
return str;
|
}
|
|
}
|