Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

      • 文本(text)模块简介
      • 字符串工具-StrUtil
        • 1. 字符串判断
          • 1.1 空值判断
          • 1.2 内容判断
        • 2. 字符串转换
          • 2.1 大小写转换
          • 2.2 格式转换
        • 3. 字符串修剪
          • 3.1 去除空白
          • 3.2 去除指定字符
        • 4. 字符串分割
          • 4.1 基础分割
          • 4.2 高级分割
        • 5. 字符串拼接
          • 5.1 基础拼接
          • 5.2 高级拼接
        • 6. 字符串替换
          • 6.1 基础替换
          • 6.2 高级替换
        • 7. 字符串截取
          • 7.1 基础截取
          • 7.2 高级截取
        • 8. 字符串填充
          • 8.1 左右填充
          • 8.2 零填充
        • 9. 其他常用功能
          • 9.1 生成随机字符串
          • 9.2 重复字符串
          • 9.3 计算字符串长度
        • 10. 适用场景
      • 字符串模板格式化和解析-StrTemplate
      • 字符序列工具-CharSequenceUtil
      • 字符工具-CharUtil
    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 字符串文本(text)
Hutool
2026-01-03
目录

字符串工具-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,开发者可以更加便捷地处理字符串,编写更简洁、更易维护的代码,同时享受到更丰富的字符串处理功能。

文本(text)模块简介
字符串模板格式化和解析-StrTemplate

← 文本(text)模块简介 字符串模板格式化和解析-StrTemplate→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式