All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.yes.tools.utils.StringUtil Maven / Gradle / Ivy
package org.yes.tools.utils;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public final class StringUtil extends org.apache.commons.lang3.StringUtils{
public static String concat(Object[] arr) {
String result = "";
for (Object item : arr) {
result.concat(String.valueOf(item));
}
return result;
}
public static boolean isEmpty(String value) {
if (value == null) {
return true;
}
if ("".equals(value)) {
return true;
}
if ("".equals(value.trim())) {
return true;
}
return value.isEmpty();
}
public static boolean isNotEmpty(String value) {
return !isEmpty(value);
}
/**
* 将字符串分割成Int列表
*
* @param parameter
* @param splitStr
* @return
*/
public static List splitString2IntList(String parameter, String splitStr) {
List ret = new ArrayList();
if (parameter == null || StringUtil.isEmpty(parameter)) {
return ret;
}
if (StringUtil.isEmpty(splitStr)) {
splitStr = "\\|";
} else if (splitStr.trim().equals("?")) {
splitStr = "\\?";
} else if (splitStr.trim().equals("*")) {
splitStr = "\\*";
}
String[] splits = parameter.split(splitStr);
try {
for (String split : splits) {
ret.add(Integer.valueOf(split.trim()));
}
} catch (NumberFormatException e) {
return new ArrayList();
}
return ret;
}
/**
* 将字符串分割成字符串列表
*
* @param parameter
* @param splitStr
* @return
*/
public static List splitString2List(String parameter, String splitStr) {
return splitString2List(parameter, splitStr, null);
}
public static List splitString2List(String parameter, String splitStr, String format) {
List ret = new ArrayList();
if (parameter == null || StringUtil.isEmpty(parameter)) {
return ret;
}
if (StringUtil.isEmpty(splitStr)) {
splitStr = "\\|";
} else if (splitStr.trim().equals("?")) {
splitStr = "\\?";
} else if (splitStr.trim().equals("*")) {
splitStr = "\\*";
}
String[] splits = parameter.split(splitStr);
try {
for (String split : splits) {
if (StringUtil.isEmpty(format)) {
ret.add(split.trim());
} else {
ret.add(String.format(format, split.trim()));
}
}
} catch (NumberFormatException e) {
return new ArrayList();
}
return ret;
}
public static String join(String spliter, List list) {
StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (T item : list) {
if (!isFirst) {
sb.append(spliter);
}
sb.append(String.valueOf(item));
isFirst = false;
}
return sb.toString();
}
public static String join(String spliter, T[] list) {
if (list == null) {
return "";
}
StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (T item : list) {
if (!isFirst) {
sb.append(spliter);
}
sb.append(String.valueOf(item));
isFirst = false;
}
return sb.toString();
}
public static Class> getClass(Type type) {
Class clz = null;
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
clz = ((Class) pt.getRawType());
} else if (type instanceof TypeVariable) {
TypeVariable tType = (TypeVariable) type;
} else {
clz = (Class) type;
}
return clz;
}
public static Object cast(Type type, String value) {
Class> clazz = getClass(type);
return cast(clazz, value);
}
public static Object cast(Class type, String value) {
if (type.isEnum()) {
try {
boolean isNumber = Pattern.compile("^\\d+$").matcher(value).find();
Object instance = Class.forName(type.getName());
String name = value;
T[] enums = type.getEnumConstants();
if (isNumber) {
Method m = type.getMethod("values");
Object[] items = (Object[]) m.invoke(instance);
for (int i = 0; i < items.length; i++) {
try {
if (value.equals(ReflectionUtil.invoke(items[i].getClass(), items[i], "getValue").toString())) {
return enums[i];
}
} catch (Exception e) {
}
try {
if (value.equals(ReflectionUtil.invoke(items[i].getClass(), items[i], "getCode").toString())) {
return enums[i];
}
} catch (Exception e) {
}
}
} else {
for (T enumObj : enums) {
if (value.toLowerCase().equals(String.valueOf(enumObj).toLowerCase())) {
return enumObj;
}
}
}
Method m = type.getMethod("valueOf", String.class);
return m.invoke(instance, name);
} catch (Exception e) {
return null;
}
}
if (type.equals(Integer.class) || type.equals(int.class)) {
try {
return Integer.parseInt(value);
} catch (Exception e) {
return 0;
}
}
if (type.equals(Long.class) || type.equals(long.class)) {
try {
return Long.parseLong(value);
} catch (Exception e) {
return 0L;
}
}
if (type.equals(Double.class) || type.equals(double.class)) {
try {
return Double.parseDouble(value);
} catch (Exception e) {
return Double.valueOf(0);
}
}
if (type.equals(Float.class) || type.equals(float.class)) {
try {
return Float.parseFloat(value);
} catch (Exception e) {
return Float.valueOf(0);
}
}
if (type.equals(BigDecimal.class)) {
try {
return BigDecimal.valueOf(Double.parseDouble(value));
} catch (Exception e) {
return BigDecimal.valueOf(Double.valueOf(0));
}
}
if (type.equals(Date.class)) {
try {
if (Pattern.matches("^\\d+$", value)) {
return new Date(Long.parseLong(value));
}
return Date.parse(value);
} catch (Exception e) {
return new Date();
}
}
if (type.equals(UUID.class)) {
try {
if (value.length() == 32) {
return UUID.fromString(value.substring(0, 8) + "-" +
value.substring(8, 12) + "-" +
value.substring(12, 16) + "-" +
value.substring(16, 20) + "-" + value.substring(20));
}
if (value.length() == 36) {
return UUID.fromString(value);
}
return value;
} catch (Exception e) {
return UUID.randomUUID();
}
}
if (type.equals(Boolean.class) || type.equals(boolean.class)) {
try {
if (value != null && value.trim().equals("1")) {
value = "true";
}
return Boolean.parseBoolean(value);
} catch (Exception e) {
return false;
}
}
if (type.equals(Byte.class) || type.equals(byte.class)) {
try {
return Byte.parseByte(value);
} catch (Exception e) {
return Byte.valueOf("");
}
}
if (type.equals(Character[].class) || type.equals(char.class)) {
try {
return value.toCharArray();
} catch (Exception e) {
return null;
}
}
if (type.equals(Byte[].class) || type.equals(byte[].class)) {
try {
return value.getBytes();
} catch (Exception e) {
return null;
}
}
if (type.equals(Blob.class)) {
try {
return new SerialBlob(value.getBytes());
} catch (SQLException e) {
return null;
}
}
if (type.equals(Clob.class)) {
try {
return new SerialClob(value.toCharArray());
} catch (SQLException e) {
return null;
}
}
if (type.equals(String.class)) {
try {
return value.toString();
} catch (Exception e) {
return "";
}
}
return value;
}
public static String toString(Object value) {
if (value == null) {
return "";
}
return value.toString();
}
public static String toLowerCase(Object value) {
return toString(value).toLowerCase();
}
public static String toUpperCase(Object value) {
return toString(value).toUpperCase();
}
public static String replaceAll(Object value, String oldChar, String newChar) {
return toString(value).replaceAll(oldChar, newChar);
}
public static String getHumpName(String name, Boolean isLowerCase) {
String result = getHumpName(name);
if (isLowerCase) {
result = result.substring(0, 1).toLowerCase() + result.substring(1);
}
return result;
}
public static String getHumpName(String name) {
List list = splitString2List(name, "_").stream().map(a -> {
if (isEmpty(a)) return "";
if (a.length() == 1) return a.toUpperCase();
return a.substring(0, 1).toUpperCase() + a.substring(1);
}).collect(Collectors.toList());
return join("", list);
}
/**
* 下划线转驼峰法(默认小驼峰)
*
* @param line 源字符串
* @param smallCamel 大小驼峰,是否为小驼峰(驼峰,第一个字符是大写还是小写)
* @return 转换后的字符串
*/
public static String underline2Camel(String line, boolean... smallCamel) {
if (isBlank(line)) {
return "";
}
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
Matcher matcher = pattern.matcher(line);
// 匹配正则表达式
while (matcher.find()) {
String word = matcher.group();
// 当是true 或则是空的情况
if ((smallCamel.length == 0 || smallCamel[0]) && matcher.start() == 0) {
sb.append(Character.toLowerCase(word.charAt(0)));
} else {
sb.append(Character.toUpperCase(word.charAt(0)));
}
int index = word.lastIndexOf('_');
if (index > 0) {
sb.append(word.substring(1, index).toLowerCase());
} else {
sb.append(word.substring(1).toLowerCase());
}
}
return sb.toString();
}
/**
* 驼峰法转下划线
*
* @param line 源字符串
* @return 转换后的字符串
*/
public static String camel2Underline(String line) {
if (isBlank(line)) {
return "";
}
line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String word = matcher.group();
sb.append(word.toUpperCase());
sb.append(matcher.end() == line.length() ? "" : "_");
}
return sb.toString();
}
}