Don’t say much, just go to the code.
<!-- commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.1</version>
</dependency>
package org.bood.common.utils;
import cn.hutool.core.text.StrFormatter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
/**
* 字符串工具类,提供了字符串的常用操作方法
*
* @author bood
* @since 2024/01/05
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private StringUtils() {
}
/**
* 判断一个字符串是否为空
*
* @param str 待判断的字符串
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
/**
* 判断一个字符串是否非空
*
* @param str 待判断的字符串
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 判断一个集合是否为空
*
* @param coll 待判断的集合
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean isEmpty(Collection<?> coll) {
return coll == null || coll.isEmpty();
}
/**
* 判断一个集合是否非空
*
* @param coll 待判断的集合
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
/**
* 判断一个 Map 是否为空
*
* @param map 待判断的 Map
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
/**
* 判断一个 Map 是否非空
*
* @param map 待判断的 Map
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* 下划线转驼峰命名
*
* @param s 待转换的下划线命名字符串
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
String[] parts = s.split("_");
StringBuilder camelCaseString = new StringBuilder(parts[0]);
for (int i = 1; i < parts.length; i++) {
String part = parts[i];
camelCaseString.append(toUpperCaseFirstChar(part));
}
return camelCaseString.toString();
}
/**
* 首字母转大写
*
* @param str 待转换的字符串
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String toUpperCaseFirstChar(String str) {
if (str == null || str.isEmpty()) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
/**
* 将字符串的首字母转换为小写
*
* @param str 待转换的字符串
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String uncapitalize(String str) {
if (isEmpty(str)) {
return str;
}
char firstChar = str.charAt(0);
if (Character.isLowerCase(firstChar)) {
return str;
}
return Character.toLowerCase(firstChar) + str.substring(1);
}
/**
* 删除字符串中的所有空格,包括中间的空格
*
* @param str 待处理的字符串
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String deleteWhitespace(String str) {
if (isEmpty(str)) {
return str;
}
return str.replace(" ", "");
}
/**
* 格式化字符串,将占位符{}替换为实际参数
*
* @param template 文本模板,包含占位符
* @param params 参数列表
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String format(String template, Object... params) {
if (isEmpty(template) || isEmpty(Arrays.asList(params))) {
return template;
}
return StrFormatter.format(template, params);
}
/**
* 反转字符串
*
* @param str 需要反转的字符串
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
/**
* 判断字符串是否匹配指定的正则表达式
*
* @param str 源字符串
* @param regex 正则表达式
* @return boolean
* @author bood
* @since 2024/01/05
*/
public static boolean matches(String str, String regex) {
if (str == null || regex == null) {
return false;
}
return str.matches(regex);
}
/**
* 计算子字符串在父字符串中出现的次数
*
* @param str 父字符串
* @param subStr 子字符串
* @return int
* @author bood
* @since 2024/01/05
*/
public static int countMatches(String str, String subStr) {
if (isEmpty(str) || isEmpty(subStr)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(subStr, idx)) != -1) {
count++;
idx += subStr.length();
}
return count;
}
/**
* URL编码
*
* @param str 待编码的字符串
* @param charset 编码格式
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String encodeUrl(String str, String charset) {
if (str == null) {
return null;
}
try {
return URLEncoder.encode(str, charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported Encoding Exception", e);
}
}
/**
* URL解码
*
* @param str 待解码的字符串
* @param charset 解码格式
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String decodeUrl(String str, String charset) {
if (str == null) {
return null;
}
try {
return URLDecoder.decode(str, charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported Encoding Exception", e);
}
}
/**
* 将字符串按指定分隔符转换为Set集合
*
* @param str 待分割的字符串
* @param sep 分隔符
* @return {@link Set }<{@link String }>
* @author bood
* @since 2024/01/05
*/
public static Set<String> str2Set(String str, String sep) {
// 此处调用str2List方法并包装为HashSet
return new HashSet<>(str2List(str, sep, true, false));
}
/**
* 将字符串按指定分隔符转换为List集合
*
* @param str 待分割的字符串
* @param sep 分隔符
* @param filterBlank 是否过滤空白字符串
* @param trim 是否去除首尾空白字符
* @return {@link List }<{@link String }>
* @author bood
* @since 2024/01/05
*/
public static List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) {
List<String> list = new ArrayList<>();
if (isEmpty(str)) {
return list;
}
// 若字符串为空或者为blank时,可直接返回空列表
if (filterBlank && isBlank(str)) {
return list;
}
// 分割字符串
String[] split = str.split(sep);
for (String string : split) {
if (filterBlank && isBlank(string)) {
continue;
}
if (trim) {
string = string.trim();
}
list.add(string);
}
return list;
}
}