package cn.huge.base.common.utils;
|
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
|
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* @title: 字符串转换工具类
|
* @description: 字符串转换工具类
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
public class StringUtils {
|
|
/**
|
* 下划线正则
|
*/
|
private static Pattern linePattern = Pattern.compile("_(\\w)");
|
|
/**
|
* 大写字母正则
|
*/
|
private static Pattern humpPattern = Pattern.compile("[A-Z]");
|
|
/**
|
* 26位小写字母
|
*/
|
private static String LOWER_CASES = "abcdefghijklmnopqrstuvwxyz";
|
|
/**
|
* 26位字母大写
|
*/
|
private static String UPER_CASES = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
/**
|
* 下划线转驼峰
|
* @param str 字符串
|
* @return String
|
*/
|
public static String lineToHump(String str) {
|
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 str 字符串
|
* @return String
|
*/
|
public static String humpToLine(String str) {
|
Matcher matcher = humpPattern.matcher(str);
|
StringBuffer sb = new StringBuffer();
|
while (matcher.find()) {
|
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
|
}
|
matcher.appendTail(sb);
|
return sb.toString();
|
}
|
|
|
/**
|
* 将JSON对象的大写转换为下划线加小写,例如:userName-->user_name
|
* @param object json对象
|
* @return String
|
* @throws JsonProcessingException
|
*/
|
public static String toUnderlineJsonString(Object object) throws JsonProcessingException {
|
ObjectMapper mapper = new ObjectMapper();
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
String reqJson = mapper.writeValueAsString(object);
|
return reqJson;
|
}
|
|
/**
|
* 将JSON下划线转换为驼峰的形式,例如:user_name-->userName
|
* @param json json字符串
|
* @param clazz 类
|
* @return T
|
* @throws IOException
|
*/
|
public static <T> T toSnakeObject(String json, Class<T> clazz) throws IOException {
|
ObjectMapper mapper = new ObjectMapper();
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
T reqJson = mapper.readValue(json, clazz);
|
return reqJson;
|
}
|
|
/**
|
* 首字母换成大写
|
* @param string
|
* @return
|
*/
|
public static String toUpperFirstLetter(String string) {
|
char[] charArray = string.toCharArray();
|
charArray[0] -= 32;
|
return String.valueOf(charArray);
|
}
|
|
/**
|
* 根据字符串获取数据集合,处理的内容包含以下格式:
|
* 用英文",“号分隔,如”1,3,5",生成1、3、5三个单元,如“A,B,E",生成A、B、E三个单元;
|
* 用英文"~"号分隔,如”1~3“,生成1、2、3三个单元;如”A-C“,生成A、B、C三个单元。
|
* @param source 单元数
|
* @return List
|
*/
|
public static List<String> getDataList(String source) {
|
List<String> dataList = new ArrayList<>();
|
try{
|
if(org.apache.commons.lang3.StringUtils.isNotEmpty(source) && source.contains("~")){
|
String dataMin = source.split("~")[0];
|
String dataMax = source.split("~")[1];
|
|
//判断字符串长度。大于1则判定为数字;等于1则转换为char,进行进一步判断
|
if(dataMin.length() == 1 && dataMax.length() ==1){
|
char charMin = dataMin.charAt(0);
|
char charMax = dataMax.charAt(0);
|
|
//判断字符是否为大写字母、小写字母、数字,然后做相应处理
|
if(Character.isLowerCase(charMin) && Character.isLowerCase(charMax)){
|
String result = LOWER_CASES.substring(LOWER_CASES.indexOf(charMin), LOWER_CASES.indexOf(charMax) + 1);
|
char datas[] = result.toCharArray();
|
for (int i = 0; i < datas.length; i++) {
|
dataList.add(String.valueOf(datas[i]));
|
}
|
}else if(Character.isUpperCase(charMin) && Character.isUpperCase(charMax)){
|
String result = UPER_CASES.substring(UPER_CASES.indexOf(charMin), UPER_CASES.indexOf(charMax) + 1);
|
char datas[] = result.toCharArray();
|
for (int i = 0; i < datas.length; i++) {
|
dataList.add(String.valueOf(datas[i]));
|
}
|
}else if(Character.isDigit(charMin) && Character.isDigit(charMax)){
|
int numMin = Integer.parseInt(dataMin);
|
int numMax = Integer.parseInt(dataMax);
|
for (int i = numMin; i <= numMax; i++){
|
dataList.add(i + "");
|
}
|
}
|
}else{
|
int numMin = Integer.parseInt(dataMin);
|
int numMax = Integer.parseInt(dataMax);
|
for (int i = numMin; i <= numMax; i++){
|
dataList.add(i + "");
|
}
|
}
|
}else if(org.apache.commons.lang3.StringUtils.isNotEmpty(source) && source.contains(",")){
|
String[] datas = source.split(",");
|
dataList = Arrays.asList(datas);
|
}else{
|
dataList.add(source);
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
return dataList;
|
}
|
|
/**
|
* 根据字符串获取数字集合,处理的内容包含以下格式:
|
* 用英文",“号分隔,如”101,103",生成101、103两个房号;
|
* 用英文"~"号分隔,如”101~1101“,生成1至11层的01号房,11个房号。
|
* 用英文"~"号分隔,如”101~1105“,生成1至11层的01至05号房,55个房号。
|
* 用英文”,“和"~"号分隔,如”101~1101,102~902“,生成1至11层的01号房
|
* @param source 单元数
|
* @return List
|
*/
|
public static List<String> getNumList(String source) {
|
List<String> numList = new ArrayList<>();
|
try{
|
if(org.apache.commons.lang3.StringUtils.isNotEmpty(source) && source.contains(",")){
|
String[] numbers = source.split(",");
|
for(String number : numbers) {
|
if(number.contains("~")){
|
numList = getList(numList, number);
|
}else{
|
numList.add(number);
|
}
|
}
|
}else if(org.apache.commons.lang3.StringUtils.isNotEmpty(source) && source.contains("~")){
|
numList = getList(numList, source);
|
}else{
|
numList.add(source);
|
}
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
return numList;
|
}
|
|
/**
|
* 解析包含 ‘~’的字符串,获取集合
|
* @param numList 数组
|
* @param number 数量
|
*/
|
private static List<String> getList(List<String> numList, String number) {
|
String numMin = number.split("~")[0];
|
String numMax = number.split("~")[1];
|
int roomMin = Integer.parseInt(numMin.substring(numMin.length() - 2));
|
int layerMin = Integer.parseInt(numMin.substring(0, numMin.length() - 2));
|
int roomMax = Integer.parseInt(numMax.substring(numMax.length() - 2));
|
int layerMax = Integer.parseInt(numMax.substring(0, numMax.length() - 2));
|
for(int i = layerMin; i <= layerMax; i++) {
|
for(int j = roomMin; j<= roomMax; j++) {
|
if(j < 10){
|
String roomNo = i + "0" + j;
|
numList.add(roomNo);
|
}else{
|
String roomNo = i + String.valueOf(j);
|
numList.add(roomNo);
|
}
|
}
|
}
|
return numList;
|
}
|
|
/**
|
* 名称太长处理
|
*/
|
public static String clipName(String name, int maxLength) {
|
if (org.apache.commons.lang3.StringUtils.isEmpty(name) || name.length() <= maxLength) {
|
return name;
|
}
|
int beginIndex = name.length() - maxLength;
|
return name.substring(beginIndex);
|
|
}
|
|
/**
|
* 判断是否纯数字
|
*/
|
public static boolean isInteger(String str) {
|
boolean result = str.matches("[0-9]+");
|
return result;
|
|
}
|
|
/**
|
* 判断字符串是不是double型
|
* @param str
|
* @return
|
*/
|
public static boolean isDouble(String str){
|
Pattern pattern = Pattern.compile("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}");
|
Matcher isNum = pattern.matcher(str);
|
if( !isNum.matches() ){
|
return false;
|
}
|
return true;
|
}
|
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|