2019-10-11 16:32:20 +08:00
|
|
|
|
package util;
|
2019-01-03 17:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2019-09-06 10:59:49 +08:00
|
|
|
|
import java.util.HashMap;
|
2019-01-03 17:14:20 +08:00
|
|
|
|
import java.util.List;
|
2019-09-06 10:59:49 +08:00
|
|
|
|
import java.util.Map;
|
2023-06-16 13:59:34 +08:00
|
|
|
|
import java.util.regex.Pattern;
|
2019-01-03 17:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @program: server
|
|
|
|
|
|
* @description: 字符串函数类
|
|
|
|
|
|
* @author: Diyigeng
|
|
|
|
|
|
* @Company: BeiJing Blue Whale Technology CO.LTD. All rights reserved
|
|
|
|
|
|
* @create: 2018-11-29 21:05
|
|
|
|
|
|
**/
|
|
|
|
|
|
public class StringUtil {
|
|
|
|
|
|
|
2023-06-16 13:59:34 +08:00
|
|
|
|
private static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?");
|
|
|
|
|
|
public static boolean isNumeric(String str) {
|
|
|
|
|
|
return str != null && NUMBER_PATTERN.matcher(str).matches();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-03 17:14:20 +08:00
|
|
|
|
public static int[] parseFiledInt(String value) {
|
2019-05-07 20:01:07 +08:00
|
|
|
|
if ("null".equals(value) || StringUtil.isEmpty(value)) {
|
2019-01-03 17:14:20 +08:00
|
|
|
|
return new int[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split("#");
|
|
|
|
|
|
int[] values = new int[param.length];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = Integer.parseInt(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int[][] parseFiledInt2(String value) {
|
|
|
|
|
|
if ("null".equals(value) || value == null) {
|
|
|
|
|
|
return new int[0][0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split("\\|");
|
|
|
|
|
|
int[][] values = new int[param.length][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledInt(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-09 15:22:55 +08:00
|
|
|
|
public static long[] parseFiledLong(String value) {
|
|
|
|
|
|
if ("null".equals(value) || StringUtil.isEmpty(value)) {
|
|
|
|
|
|
return new long[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split("#");
|
|
|
|
|
|
long[] values = new long[param.length];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = Long.parseLong(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static long[][] parseFiledLong2(String value) {
|
|
|
|
|
|
if ("null".equals(value) || value == null) {
|
|
|
|
|
|
return new long[0][0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split("\\|");
|
|
|
|
|
|
long[][] values = new long[param.length][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledLong(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-03 17:14:20 +08:00
|
|
|
|
public static int[][][] parseFiledInt3(String value) {
|
|
|
|
|
|
if ("null".equals(value) || value == null) {
|
|
|
|
|
|
return new int[0][0][0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split(",");
|
|
|
|
|
|
int[][][] values = new int[param.length][][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledInt2(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static float[] parseFiledFloat(String value) {
|
|
|
|
|
|
if ("null".equals(value) || value == null) {
|
|
|
|
|
|
return new float[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split("#");
|
|
|
|
|
|
float[] values = new float[param.length];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = Float.parseFloat(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static float[][] parseFiledFloat2(String value) {
|
|
|
|
|
|
if ("null".equals(value) || value == null) {
|
|
|
|
|
|
return new float[0][0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split("\\|");
|
|
|
|
|
|
float[][] values = new float[param.length][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledFloat(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static float[][][] parseFiledFloat3(String value) {
|
|
|
|
|
|
if ("null".equals(value) || value == null) {
|
|
|
|
|
|
return new float[0][0][0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split(",");
|
|
|
|
|
|
float[][][] values = new float[param.length][][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledFloat2(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-10 16:09:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* a#a|b#b 格式字符串转二维数组
|
|
|
|
|
|
*/
|
2024-04-20 18:04:17 +08:00
|
|
|
|
public static String[] parseFiledString(String value){
|
|
|
|
|
|
if ("".equals(value) || value == null) {
|
|
|
|
|
|
return new String[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
return value.split("#");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String[][] parseFiledString2(String value){
|
2021-03-10 16:09:24 +08:00
|
|
|
|
if ("".equals(value) || value == null) {
|
|
|
|
|
|
return new String[0][0];
|
|
|
|
|
|
}
|
2024-04-20 18:04:17 +08:00
|
|
|
|
String[] param = value.split("\\|");
|
|
|
|
|
|
String[][] values = new String[param.length][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledString(param[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String[][][] parseFiledString3(String value){
|
|
|
|
|
|
if ("".equals(value) || value == null) {
|
|
|
|
|
|
return new String[0][0][0];
|
|
|
|
|
|
}
|
|
|
|
|
|
String[] param = value.split(",");
|
|
|
|
|
|
String[][][] values = new String[param.length][][];
|
|
|
|
|
|
for (int i = 0; i < param.length; i++) {
|
|
|
|
|
|
values[i] = parseFiledString2(param[i]);
|
2021-03-10 16:09:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
2019-01-03 17:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 读取配置表将首字母小写,如果只有两个字母则全变为小写
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String[] fieldHandle(String[] params) {
|
|
|
|
|
|
String[] params1 = new String[params.length];
|
|
|
|
|
|
for (int i = 0; i < params.length; i++) {
|
|
|
|
|
|
if (params[i].length() <= 2) {
|
|
|
|
|
|
params1[i] = params[i].toLowerCase();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
params1[i] = (new StringBuilder()).append(Character.toLowerCase(params[i].charAt(0))).append(params[i].substring(1)).toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return params1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int[] getIntArray(String str, String sep) {
|
|
|
|
|
|
String[] prop = getStringList(str, sep);
|
|
|
|
|
|
List<Integer> tmp = new ArrayList<Integer>();
|
|
|
|
|
|
for (int i = 0; i < prop.length; i++) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
int r = Integer.parseInt(prop[i]);
|
|
|
|
|
|
tmp.add(r);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
int[] ints = new int[tmp.size()];
|
|
|
|
|
|
for (int i = 0; i < tmp.size(); i++) {
|
|
|
|
|
|
ints[i] = tmp.get(i);
|
|
|
|
|
|
}
|
|
|
|
|
|
return ints;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String[] getStringList(String str, String sep) {
|
|
|
|
|
|
str = trim(str);
|
|
|
|
|
|
return str.split(sep);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String trim(String str) {
|
|
|
|
|
|
if (str == null) {
|
|
|
|
|
|
str = "";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
str = str.trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (str.length() == 0) {
|
|
|
|
|
|
return str;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (str.charAt(0) == '"') {
|
|
|
|
|
|
str = str.substring(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (str.charAt(str.length() - 1) == '"') {
|
|
|
|
|
|
str = str.substring(0, str.length() - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
|
}
|
2019-01-22 14:44:02 +08:00
|
|
|
|
|
|
|
|
|
|
public static boolean isEmpty(String source){
|
2023-10-20 16:58:20 +08:00
|
|
|
|
return source == null || source.isEmpty();
|
2019-01-22 14:44:02 +08:00
|
|
|
|
}
|
2019-05-13 15:28:03 +08:00
|
|
|
|
|
|
|
|
|
|
public static String parseArrayToString(int[][] source) {
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-06-05 20:15:56 +08:00
|
|
|
|
int length = source.length;
|
|
|
|
|
|
int last = length-1;
|
|
|
|
|
|
for(int i=0;i<length;i++){
|
|
|
|
|
|
int[] item = source[i];
|
2021-03-03 17:16:03 +08:00
|
|
|
|
int itemLast = item.length;
|
2019-06-05 20:15:56 +08:00
|
|
|
|
if(i==last){
|
2021-03-03 17:16:03 +08:00
|
|
|
|
for (int j = 0; j < itemLast; j++) {
|
|
|
|
|
|
if (j == itemLast-1){
|
|
|
|
|
|
sb.append(item[j]);
|
|
|
|
|
|
}else {
|
|
|
|
|
|
sb.append(item[j]).append("#");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-06-05 20:15:56 +08:00
|
|
|
|
}else{
|
2021-03-03 17:16:03 +08:00
|
|
|
|
for (int j = 0; j < itemLast; j++) {
|
|
|
|
|
|
if (j == itemLast-1){
|
|
|
|
|
|
sb.append(item[j]).append("|");
|
|
|
|
|
|
}else {
|
|
|
|
|
|
sb.append(item[j]).append("#");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-06-05 20:15:56 +08:00
|
|
|
|
}
|
2019-05-13 15:28:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
return sb.toString();
|
2025-12-16 13:59:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String parseLongArrayToString(long[][] source) {
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
int length = source.length;
|
|
|
|
|
|
int last = length-1;
|
|
|
|
|
|
for(int i=0;i<length;i++){
|
|
|
|
|
|
long[] item = source[i];
|
|
|
|
|
|
int itemLast = item.length;
|
|
|
|
|
|
if(i==last){
|
|
|
|
|
|
for (int j = 0; j < itemLast; j++) {
|
|
|
|
|
|
if (j == itemLast-1){
|
|
|
|
|
|
sb.append(item[j]);
|
|
|
|
|
|
}else {
|
|
|
|
|
|
sb.append(item[j]).append("#");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
|
|
for (int j = 0; j < itemLast; j++) {
|
|
|
|
|
|
if (j == itemLast-1){
|
|
|
|
|
|
sb.append(item[j]).append("|");
|
|
|
|
|
|
}else {
|
|
|
|
|
|
sb.append(item[j]).append("#");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return sb.toString();
|
2019-05-13 15:28:03 +08:00
|
|
|
|
}
|
2019-09-06 11:04:28 +08:00
|
|
|
|
|
2019-09-06 11:09:05 +08:00
|
|
|
|
|
2019-09-06 11:04:28 +08:00
|
|
|
|
public static String getServiceKey(String serviceName, String host, String port) {
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
return sb.append(serviceName).append("|")
|
|
|
|
|
|
.append(host).append("|")
|
|
|
|
|
|
.append(port).toString();
|
|
|
|
|
|
}
|
2019-09-06 11:09:05 +08:00
|
|
|
|
|
2019-09-06 10:59:49 +08:00
|
|
|
|
public static Map<Integer,Integer> parsent2ToIntMap(int[][] value) {
|
|
|
|
|
|
Map<Integer, Integer> result = new HashMap<>();
|
|
|
|
|
|
int length = value.length;
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
|
int[] item = value[i];
|
|
|
|
|
|
if (item.length != 2) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
result.put(item[0], item[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2021-05-15 15:05:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 一个将Int数组转String的方法
|
|
|
|
|
|
* @param intArr
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static StringBuffer intArrToStringArr(int[] intArr) {
|
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
|
|
for(int num : intArr){
|
|
|
|
|
|
stringBuffer.append(num);
|
|
|
|
|
|
}
|
|
|
|
|
|
return stringBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2019-01-03 17:14:20 +08:00
|
|
|
|
}
|