import com.common.ConstantFactory;
import com.common.security.MD5Util;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* 字符串处理类
* @author carrot
* @date 2024-5-12
*/
public final class StringUtil {
//region 常量
private static final String REGX_MOBILEPHONE = "^((\\+86)|(\\(\\+86\\))-)?[0-9]{11}$";
private static final String REGX_TELEPHONE = "^((\\+86)|(\\(\\+86\\))-)?(((0[1,2]{1}\\d{1})?-?\\d{8})|((0[3-9]{1}\\d{2})?-?\\d{7,8}))$";
private static final String REGX_IDCARD = "^([0-9]{17}[0-9X]{1})|([0-9]{15})$";
private static final String REGX_WEBURL = "^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\\.))+(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(/[a-zA-Z0-9\\&%_\\./-~-]*)?$";
private static final String REGX_EMAIL = "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
private static final String REGX_MONEY = "^\\d{1,12}(?:\\.\\d{1,4})?$";
private static final String DEFAULT = "utf-8";
//endregion
private StringUtil() {
throw new IllegalStateException("Utility class");
}
/**
* 从当前对象移除头尾指定字符
* @author xiangyuanzhang
* @date 2018-12-25
* @param source 原始字符串
* @param trimChar 移除字符串
*/
public String trim(String source, String trimChar) {
if(source==null){
return "";
}
source = source.trim();
if(source.isEmpty()){
return "";
}
String beginChar = source.substring(0, 1);
if (beginChar.equalsIgnoreCase(trimChar)) {
source = source.substring(1, source.length());
}
String endChar = source.substring(source.length() - 1, source.length());
if (endChar.equalsIgnoreCase(trimChar)) {
source = source.substring(0, source.length() - 1);
}
return source;
}
/**
* 从当前对象移除头部指定字符
* @author xiangyuanzhang
* @date 2018-12-25
* @param source 原始字符串
* @param trimChar 移除字符串
*/
public String trimStart(String source, String trimChar) {
if(source==null){
return "";
}
source = source.trim();
if(source.isEmpty()){
return "";
}
String beginChar = source.substring(0, 1);
if (beginChar.equalsIgnoreCase(trimChar)) {
source = source.substring(1, source.length());
}
return source;
}
/**
* 从当前对象移除尾部指定字符
* @author xiangyuanzhang
* @date 2018-12-25
* @param source 原始字符串
* @param trimChar 移除字符串
*/
public String trimEnd(String source, String trimChar) {
if(source==null){
return "";
}
source = source.trim();
if(source.isEmpty()){
return "";
}
String endChar = source.substring(source.length() - 1, source.length());
if (endChar.equalsIgnoreCase(trimChar)) {
source = source.substring(0, source.length() - 1);
}
return source;
}
/**
* 字符串枚举值转成枚举对象
* @author linliu
* @date 2018-12-24
* @param enumType 枚举类型
* @param value 枚举值
* @param <T> 枚举类型
* @return 枚举对象
*/
public static <T extends Enum<T>> T parseEnum(Class<T> enumType, String value) {
if(isNullOrEmpty(value)) {
return null;
}
T result = null;
try {
T[] values = enumType.getEnumConstants();
Method getValue = enumType.getMethod("getValue");
for (T e : values) {
if(getValue.invoke(e).toString().equals(value)) {
result = e;
break;
}
}
} catch (Exception e) {
//TODO log
}
return result;
}
/**
* 字符串枚举值转成枚举对象
java字符串工具类String
于 2024-05-12 17:38:12 首次发布