StrUtil 是 Hutool 工具库中处理字符串的核心工具类,提供了大量静态方法,弥补了 Java 原生字符串处理的不足。
Maven 依赖
XML
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
常用方法
1. 判空方法
java
// 判断是否为 null 或空字符串
StrUtil.isEmpty(""); // true
StrUtil.isEmpty(" "); // false(空格不是空)
StrUtil.isEmpty(null); // true
// 判断是否为 null 或空白字符(空格、制表符等)
StrUtil.isBlank(" "); // true
StrUtil.isBlank("\t\n"); // true
StrUtil.isBlank("abc"); // false
// 非空判断(取反)
StrUtil.isNotEmpty("abc"); // true
StrUtil.isNotBlank("abc"); // true
// 判断是否全部为空
StrUtil.hasEmpty("a", "", "c"); // true
StrUtil.hasBlank("a", " ", "c"); // true
// 判断是否全部非空
StrUtil.isAllNotEmpty("a", "b", "c"); // true
2. 字符串截取
java
String str = "abcdefgh";
// 截取指定位置
StrUtil.sub(str, 2, 5); // "cde"(从索引2到5,不含5)
StrUtil.sub(str, 2, -2); // "cdef"(负索引从末尾计数)
// 截取前缀/后缀
StrUtil.subPre(str, 3); // "abc"(前3个字符)
StrUtil.subSuf(str, 3); // "defgh"(从索引3开始到末尾)
StrUtil.subSufByLength(str, 3); // "fgh"(最后3个字符)
// 截取指定字符串之前/之后
StrUtil.subBefore("abcde", "cd", false); // "ab"(首次出现之前)
StrUtil.subAfter("abcde", "cd", false); // "e"(首次出现之后)
StrUtil.subBetween("abccd", "b", "d"); // "cc"(两个字符串之间)
3. 字符串格式化
java
// 格式化模板(占位符用 {})
StrUtil.format("{} + {} = {}", 1, 2, 3); // "1 + 2 = 3"
// 带索引的占位符
StrUtil.format("{1} + {2} = {0}", 3, 1, 2); // "1 + 2 = 3"
// 格式化金额
StrUtil.format("#,###,###.##", 1234567.89); // "1,234,567.89"
4. 字符串填充
java
// 左填充
StrUtil.fillBefore("abc", '0', 5); // "00abc"
// 右填充
StrUtil.fillAfter("abc", '0', 5); // "abc00"
// 居中填充
StrUtil.fillCenter("abc", '0', 5); // "0abc0"
StrUtil.fillCenter("abc", '0', 6); // "0abc00"
5. 字符串缩写
java
// 缩略字符串(超出部分用...代替)
StrUtil.str("这是一段很长的文本", 5); // "这是一段很..."
StrUtil.str("短文本", 10); // "短文本"
// 自定义缩略符
StrUtil.str("这是一段很长的文本", 5, "..."); // "这是一段很..."
6. 移除前缀/后缀
java
StrUtil.removePrefix("www.example.com", "www."); // "example.com"
StrUtil.removeSuffix("example.jpg", ".jpg"); // "example"
StrUtil.removePrefixIgnoreCase("WWW.example.com", "www."); // "example.com"
// 移除所有匹配项
StrUtil.strip("abcabc", "ab"); // "cabc"(只移除第一个匹配)
7. 字符串转换
java
// 驼峰转下划线
StrUtil.toUnderlineCase("helloWorld"); // "hello_world"
// 下划线转驼峰
StrUtil.toCamelCase("hello_world"); // "helloWorld"
// 首字母大写
StrUtil.upperFirst("hello"); // "Hello"
// 首字母小写
StrUtil.lowerFirst("Hello"); // "hello"
// 全角转半角
StrUtil.toHalfWidth("123"); // "123"
// 半角转全角
StrUtil.toFullWidth("123"); // "123"
8. 字符串掩码
java
String str = "ababa";
// 查找字符位置
StrUtil.indexOf(str, 'b'); // 1(首次出现)
StrUtil.lastIndexOf(str, 'b'); // 3(最后出现)
// 替换
StrUtil.replace("abcabc", "ab", "xy"); // "xycxyc"
StrUtil.replaceIgnoreCase("Abc", "ab", "xy"); // "xyc"
// 替换指定位置
StrUtil.replace("abcdef", 2, 4, "**"); // "ab**ef"
9. 查找与替换
java
// 隐藏部分字符
StrUtil.hide("13812345678", 3, 7); // "138****5678"
StrUtil.hide("张三丰", 1, 2); // "张*丰"
// 自定义掩码字符
StrUtil.hide("13812345678", 3, 7, '#'); // "138####5678"
10. 字符串比较
java
// 是否为空或null
StrUtil.isEmpty(null); // true
// 是否相等(支持null)
StrUtil.equals("abc", "abc"); // true
StrUtil.equalsIgnoreCase("abc", "ABC"); // true
// 是否包含(支持null)
StrUtil.contains("abcdef", "cde"); // true
StrUtil.containsIgnoreCase("Abc", "ab"); // true
// 是否以某字符串开头/结尾
StrUtil.startWith("abcdef", "abc"); // true
StrUtil.endWith("abcdef", "def"); // true
11. 拼接与合并
java
// 拼接
StrUtil.concat(true, "a", "b", "c"); // "abc"
// 连接(可指定分隔符)
StrUtil.join(",", "a", "b", "c"); // "a,b,c"
// 连接数组
String[] arr = {"a", "b", "c"};
StrUtil.join(",", arr); // "a,b,c"
// 连接集合
List<String> list = Arrays.asList("a", "b", "c");
StrUtil.join(",", list); // "a,b,c"
12. 其他实用方法
java
// 获取字符串长度(支持null)
StrUtil.length(null); // 0
StrUtil.length("abc"); // 3
// 重复字符串
StrUtil.repeat("ab", 3); // "ababab"
// 去除换行符
StrUtil.cleanBlank("a\nb\r\nc"); // "abc"
// 反转字符串
StrUtil.reverse("abc"); // "cba"
// 截取指定编码长度
StrUtil.subByCodePoint("abc", 2); // "ab"
// 交换大小写
StrUtil.swapCase("Hello"); // "hELLO"
// 统计字符出现次数
StrUtil.count("ababab", 'a'); // 3
完整示例
java
import cn.hutool.core.util.StrUtil;
public class StrUtilDemo {
public static void main(String[] args) {
// 判空
System.out.println(StrUtil.isBlank(" ")); // true
// 格式化
System.out.println(StrUtil.format("姓名:{},年龄:{}", "张三", 25));
// 驼峰转换
System.out.println(StrUtil.toUnderlineCase("userName")); // user_name
// 掩码
System.out.println(StrUtil.hide("13812345678", 3, 7)); // 138****5678
// 连接
System.out.println(StrUtil.join("-", "2024", "01", "15")); // 2024-01-15
// 缩写
System.out.println(StrUtil.str("这是一段很长的文本内容", 8)); // 这是一段很...
}
}
总结
StrUtil 涵盖了日常开发中 90% 的字符串操作需求,建议:
-
优先使用
isBlank而非isEmpty(更符合业务场景) -
使用
format替代字符串拼接,提高可读性 -
利用
hide方法处理敏感信息脱敏