字符串工具-StrUtil
StrUtil是Hutool中最核心的字符串处理工具类,提供了丰富的字符串操作方法,涵盖了字符串判断、转换、分割、替换、拼接等各个方面,让字符串处理变得更加便捷和高效。
# 1. 字符串判断
# 1.1 空值判断
// 判断字符串是否为null或空字符串
boolean isEmpty = StrUtil.isEmpty("";
boolean isEmpty = StrUtil.isEmpty(null);
// 判断字符串是否为null、空字符串或空白字符
boolean isBlank = StrUtil.isBlank(" ");
// 判断字符串是否不为空
boolean isNotEmpty = StrUtil.isNotEmpty("abc");
// 判断字符串是否不为空白
boolean isNotBlank = StrUtil.isNotBlank("abc");
# 1.2 内容判断
// 判断字符串是否以指定前缀开头
boolean startsWith = StrUtil.startWith("abc", "a");
// 判断字符串是否以指定后缀结尾
boolean endsWith = StrUtil.endWith("abc", "c");
// 判断字符串是否包含指定子串
boolean contains = StrUtil.contains("abc", "b");
// 判断字符串是否等于指定值(支持null安全比较)
boolean equals = StrUtil.equals("abc", "abc");
# 2. 字符串转换
# 2.1 大小写转换
// 转换为大写
String upper = StrUtil.upperFirst("abc");
// 转换为小写
String lower = StrUtil.lowerFirst("ABC");
// 首字母大写
String capitalize = StrUtil.capitalize("abc");
// 首字母小写
String uncapitalize = StrUtil.uncapitalize("ABC");
# 2.2 格式转换
// 格式化字符串
String formatted = StrUtil.format("Hello, {}!", "world");
// 格式化字符串(支持命名参数)
Map<String, Object> params = new HashMap<>();
params.put("name", "world");
String formatted = StrUtil.format("Hello, ${name}!", params);
# 3. 字符串修剪
# 3.1 去除空白
// 去除字符串两端空白
String trimmed = StrUtil.trim(" abc ");
// 去除字符串左端空白
String ltrimmed = StrUtil.ltrim(" abc ");
// 去除字符串右端空白
String rtrimmed = StrUtil.rtrim(" abc ");
// 去除字符串中所有空白
String cleaned = StrUtil.cleanBlank("a b c");
# 3.2 去除指定字符
// 去除字符串两端指定字符
String stripped = StrUtil.strip("'abc'", "'");
// 去除字符串左端指定字符
String lstripped = StrUtil.lstrip("'abc'", "'");
// 去除字符串右端指定字符
String rstripped = StrUtil.rstrip("'abc'", "'");
# 4. 字符串分割
# 4.1 基础分割
// 按指定分隔符分割字符串
String[] parts = StrUtil.split("a,b,c", ',');
// 按指定分隔符分割字符串,限制分割数量
String[] parts = StrUtil.split("a,b,c,d", ',', 2);
// 分割为List
List<String> partList = StrUtil.splitToList("a,b,c", ',');
# 4.2 高级分割
// 按空白字符分割
String[] parts = StrUtil.split("a b c");
// 按换行符分割
String[] lines = StrUtil.split("a\nb\nc", true);
// 安全分割(避免空指针)
String[] parts = StrUtil.splitNullSafe(null, ',');
# 5. 字符串拼接
# 5.1 基础拼接
// 拼接字符串
String joined = StrUtil.join(",", "a", "b", "c");
// 拼接数组元素
String[] array = {"a", "b", "c"};
String joined = StrUtil.join(",", array);
// 拼接集合元素
List<String> list = Arrays.asList("a", "b", "c");
String joined = StrUtil.join(",", list);
# 5.2 高级拼接
// 前置拼接
String prefixed = StrUtil.prependIfMissing("abc", "prefix_");
// 后置拼接
String suffixed = StrUtil.appendIfMissing("abc", "_suffix");
# 6. 字符串替换
# 6.1 基础替换
// 替换指定子串
String replaced = StrUtil.replace("abc", "b", "B");
// 替换所有匹配的子串
String replaced = StrUtil.replaceAll("ababa", "a", "A");
// 替换第一个匹配的子串
String replaced = StrUtil.replaceFirst("ababa", "a", "A");
# 6.2 高级替换
// 使用正则表达式替换
String replaced = StrUtil.replaceRegex("abc123", "\\d+", "");
// 替换字符串中的变量
Map<String, Object> params = new HashMap<>();
params.put("name", "world");
String replaced = StrUtil.replace("Hello, ${name}!");
# 7. 字符串截取
# 7.1 基础截取
// 截取指定位置的子串
String sub = StrUtil.sub("abcdef", 1, 4);
// 从指定位置截取到字符串末尾
String sub = StrUtil.subSuf("abcdef", 2);
// 截取字符串开头指定长度的子串
String sub = StrUtil.subPre("abcdef", 3);
# 7.2 高级截取
// 截取字符串中两个子串之间的内容
String between = StrUtil.subBetween("<tag>content</tag>", "<tag>", "</tag>");
// 截取字符串中指定前缀之后的内容
String after = StrUtil.subAfter("abcdef", "c", false);
// 截取字符串中指定后缀之前的内容
String before = StrUtil.subBefore("abcdef", "d", false);
# 8. 字符串填充
# 8.1 左右填充
// 左填充指定字符到指定长度
String padded = StrUtil.padLeft("123", 5, '0');
// 右填充指定字符到指定长度
String padded = StrUtil.padRight("123", 5, '0');
// 居中填充指定字符到指定长度
String padded = StrUtil.padCenter("123", 5, '0');
# 8.2 零填充
// 左填充零到指定长度
String padded = StrUtil.fillZero("123", 5);
# 9. 其他常用功能
# 9.1 生成随机字符串
// 生成指定长度的随机字符串
String random = StrUtil.random(5);
// 生成指定长度的随机数字字符串
String randomNumbers = StrUtil.randomNumbers(5);
// 生成指定长度的随机字母字符串
String randomLetters = StrUtil.randomLetters(5);
# 9.2 重复字符串
// 重复指定次数的字符串
String repeated = StrUtil.repeat("abc", 3);
# 9.3 计算字符串长度
// 计算字符串长度(支持Unicode)
int length = StrUtil.length("你好,世界");
# 10. 适用场景
StrUtil适用于以下场景:
- 需要进行大量字符串处理时
- 需要简化字符串操作API调用时
- 需要使用常用的字符串判断、转换、分割、替换等功能时
- 需要处理空白字符、特殊字符时
- 需要生成随机字符串时
- 需要进行字符串格式化时
通过StrUtil,开发者可以更加便捷地处理字符串,编写更简洁、更易维护的代码,同时享受到更丰富的字符串处理功能。