StringUtils 更新一些标为弃用的方法

This commit is contained in:
thinkgem
2025-10-10 19:25:41 +08:00
parent 1199d947ff
commit d475e154a6

View File

@@ -7,6 +7,8 @@ package com.jeesite.common.lang;
import com.jeesite.common.codec.EncodeUtils; import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.collect.ListUtils; import com.jeesite.common.collect.ListUtils;
import org.apache.commons.lang3.Strings; import org.apache.commons.lang3.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -18,10 +20,12 @@ import java.util.regex.Pattern;
/** /**
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类 * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
* @author ThinkGem * @author ThinkGem
* @version 2018-1-6 * @version 2025-10-10
*/ */
public class StringUtils extends org.apache.commons.lang3.StringUtils { public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final Logger logger = LoggerFactory.getLogger(StringUtils.class);
public static final String DOT = "."; public static final String DOT = ".";
public static final String COMMA = ","; public static final String COMMA = ",";
public static final String COLON = ":"; public static final String COLON = ":";
@@ -57,10 +61,10 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* 转换为字节数组 * 转换为字节数组
* @param str 字符串 * @param str 字符串
*/ */
public static byte[] getBytes(String str){ public static byte[] getBytes(String str) {
if (str != null){ if (str != null) {
return str.getBytes(StandardCharsets.UTF_8); return str.getBytes(StandardCharsets.UTF_8);
}else{ } else {
return null; return null;
} }
} }
@@ -69,7 +73,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* 转换为字节数组 * 转换为字节数组
* @param bytes 字节数组 * @param bytes 字节数组
*/ */
public static String toString(byte[] bytes){ public static String toString(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8); return new String(bytes, StandardCharsets.UTF_8);
} }
@@ -79,10 +83,10 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @param strs 字符串组 * @param strs 字符串组
* @return 包含返回true * @return 包含返回true
*/ */
public static boolean inString(String str, String... strs){ public static boolean inString(String str, String... strs) {
if (str != null && strs != null){ if (str != null && strs != null) {
for (String s : strs){ for (String s : strs) {
if (str.equals(trim(s))){ if (str.equals(trim(s))) {
return true; return true;
} }
} }
@@ -96,10 +100,10 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @param strs 字符串组 * @param strs 字符串组
* @return 包含返回true * @return 包含返回true
*/ */
public static boolean inStringIgnoreCase(String str, String... strs){ public static boolean inStringIgnoreCase(String str, String... strs) {
if (str != null && strs != null){ if (str != null && strs != null) {
for (String s : strs){ for (String s : strs) {
if (str.equalsIgnoreCase(trim(s))){ if (str.equalsIgnoreCase(trim(s))) {
return true; return true;
} }
} }
@@ -114,6 +118,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return Strings.CS.equals(cs1, cs2); return Strings.CS.equals(cs1, cs2);
} }
/**
* 比较字符串,是否相等,只要有一个匹配就成立
*/
public static boolean equalsAny(final CharSequence string, final CharSequence... searchStrings) {
return Strings.CS.equalsAny(string, searchStrings);
}
/** /**
* 比较字符串,是否相等(忽略大小写) * 比较字符串,是否相等(忽略大小写)
*/ */
@@ -128,6 +139,20 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return Strings.CS.contains(seq, searchSeq); return Strings.CS.contains(seq, searchSeq);
} }
/**
* 比较字符串,是否在字符串中包含,只要有一个匹配就成立
*/
public static boolean containsAny(final CharSequence cs, final CharSequence... searchCharSequences) {
return Strings.CS.containsAny(cs, searchCharSequences);
}
/**
* 比较字符串,是否在字符串中包含(忽略大小写)
*/
public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr) {
return Strings.CI.contains(str, searchStr);
}
/** /**
* 比较字符串,前缀是否匹配 * 比较字符串,前缀是否匹配
*/ */
@@ -177,6 +202,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return Strings.CS.replace(text, searchString, replacement); return Strings.CS.replace(text, searchString, replacement);
} }
/**
* 替换字符串(忽略大小写)
*/
public static String replaceIgnoreCase(final String text, final String searchString, final String replacement) {
return Strings.CI.replace(text, searchString, replacement);
}
/** /**
* 去除左右空格(包含中文空格) * 去除左右空格(包含中文空格)
*/ */
@@ -188,7 +220,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* 替换掉HTML标签方法 * 替换掉HTML标签方法
*/ */
public static String stripHtml(String html) { public static String stripHtml(String html) {
if (isBlank(html)){ if (isBlank(html)) {
return EMPTY; return EMPTY;
} }
//html.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", ""); //html.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "");
@@ -201,8 +233,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 替换为手机识别的HTML去掉样式及属性保留回车。 * 替换为手机识别的HTML去掉样式及属性保留回车。
*/ */
public static String toMobileHtml(String html){ public static String toMobileHtml(String html) {
if (isBlank(html)){ if (isBlank(html)) {
return EMPTY; return EMPTY;
} }
return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>"); return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>");
@@ -211,8 +243,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 对txt进行HTML编码并将\n转换为&gt;br/&lt;、\t转换为&nbsp; &nbsp; * 对txt进行HTML编码并将\n转换为&gt;br/&lt;、\t转换为&nbsp; &nbsp;
*/ */
public static String toHtml(String txt){ public static String toHtml(String txt) {
if (isBlank(txt)){ if (isBlank(txt)) {
return EMPTY; return EMPTY;
} }
return replace(replace(EncodeUtils.encodeHtml(trim(txt)), "\n", "<br/>"), "\t", "&nbsp; &nbsp; "); return replace(replace(EncodeUtils.encodeHtml(trim(txt)), "\n", "<br/>"), "\t", "&nbsp; &nbsp; ");
@@ -224,7 +256,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @param length 截取长度 * @param length 截取长度
*/ */
public static String abbr(String str, int length) { public static String abbr(String str, int length) {
if (isBlank(str)){ if (isBlank(str)) {
return EMPTY; return EMPTY;
} }
try { try {
@@ -241,13 +273,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
} }
return sb.toString(); return sb.toString();
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
return EMPTY; return EMPTY;
} }
// 缩略字符串替换Html正则表达式预编译 // 缩略字符串替换Html正则表达式预编译
private static Pattern p1 = Pattern.compile("<([a-zA-Z]+)[^<>]*>"); private static final Pattern p1 = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
/** /**
* 缩略字符串适应于与HTML标签的 * 缩略字符串适应于与HTML标签的
@@ -255,7 +287,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @param length 截取长度 * @param length 截取长度
*/ */
public static String htmlAbbr(String param, int length) { public static String htmlAbbr(String param, int length) {
if (isBlank(param)){ if (isBlank(param)) {
return EMPTY; return EMPTY;
} }
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
@@ -280,7 +312,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
n += String.valueOf(temp).getBytes("GBK").length; n += String.valueOf(temp).getBytes("GBK").length;
} }
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
if (n <= length - 3) { if (n <= length - 3) {
result.append(temp); result.append(temp);
@@ -316,21 +348,20 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 首字母大写 * 首字母大写
*/ */
public static String cap(String str){ public static String cap(String str) {
return capitalize(str); return capitalize(str);
} }
/** /**
* 首字母小写 * 首字母小写
*/ */
public static String uncap(String str){ public static String uncap(String str) {
return uncapitalize(str); return uncapitalize(str);
} }
/** /**
* 转换为驼峰命名法 * 转换为驼峰命名法
* @return * @return camelCase("hello_world") == "helloWorld"
* camelCase("hello_world") == "helloWorld"
* capCamelCase("hello_world") == "HelloWorld" * capCamelCase("hello_world") == "HelloWorld"
* uncamelCase("helloWorld") = "hello_world" * uncamelCase("helloWorld") = "hello_world"
*/ */
@@ -359,8 +390,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 转换为驼峰命名法(首字母大写) * 转换为驼峰命名法(首字母大写)
* @return *
* camelCase("hello_world") == "helloWorld" * @return camelCase("hello_world") == "helloWorld"
* capCamelCase("hello_world") == "HelloWorld" * capCamelCase("hello_world") == "HelloWorld"
* uncamelCase("helloWorld") = "hello_world" * uncamelCase("helloWorld") = "hello_world"
*/ */
@@ -370,8 +401,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 取消驼峰命名发 * 取消驼峰命名发
* @return *
* camelCase("hello_world") == "helloWorld" * @return camelCase("hello_world") == "helloWorld"
* capCamelCase("hello_world") == "HelloWorld" * capCamelCase("hello_world") == "HelloWorld"
* uncamelCase("helloWorld") = "hello_world" * uncamelCase("helloWorld") = "hello_world"
*/ */
@@ -406,7 +437,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* 例如row.user.id * 例如row.user.id
* 返回:!row?'':!row.user?'':!row.user.id?'':row.user.id * 返回:!row?'':!row.user?'':!row.user.id?'':row.user.id
*/ */
public static String jsGetVal(String objectString){ public static String jsGetVal(String objectString) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
StringBuilder val = new StringBuilder(); StringBuilder val = new StringBuilder();
String[] vals = split(objectString, "."); String[] vals = split(objectString, ".");
@@ -425,7 +456,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
public static String getRandomStr(int count) { public static String getRandomStr(int count) {
char[] codeSeq = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', char[] codeSeq = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
return getRandom(count, codeSeq); return getRandom(count, codeSeq);
} }
@@ -440,6 +471,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 获取随机字符串 * 获取随机字符串
*
* @param count 长度 * @param count 长度
* @param codeSeq 因子 * @param codeSeq 因子
*/ */