Class StrUtil
StrUtil 设计。
提供判空、去空白、截取、拼接、格式化、驼峰转换等常用静态方法。
English: String utilities: emptiness checks, trimming, splitting, joining, casing and formatting.
- Since:
- 0.1.0
- Author:
- suretool
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringabbreviate(CharSequence str, int maxWidth) 截断字符串:超过 maxWidth 时保留前 maxWidth-3 个字符并以...结尾。static Stringabbreviate(CharSequence str, int offset, int maxWidth) 截断字符串:从指定偏移开始,超过 maxWidth 时以...结尾。static StringabbreviateMiddle(CharSequence str, String middle, int length) 中间省略:保留首尾字符,中间以指定省略串连接(参考 commons-langStringUtils.abbreviateMiddle)。static StringblankToDefault(CharSequence str, String defaultStr) 空白字符串使用默认值替换。static StringblankToDefault(String str, String defaultValue) 空字符串(含空白)返回默认值,否则原样返回。static byte[]bytes(CharSequence str, Charset charset) 字符串转字节数组。static String将字符串居中对齐,两侧以指定字符填充。static String将字符串居中对齐,两侧以指定字符串填充。static StringcleanBlank(CharSequence str) 去除字符串中所有空白字符(包括空格、制表符、换行等)。static String拼接多个字符串为一个字符串。static booleancontains(CharSequence str, CharSequence searchStr) 是否包含指定子串。static booleancontainsAny(CharSequence str, CharSequence... searchStr) 是否包含任一指定子串。static booleancontainsAnyIgnoreCase(CharSequence str, CharSequence... searchStrs) 忽略大小写判断字符串是否包含任意给定子串。static booleancontainsIgnoreCase(CharSequence str, CharSequence searchStr) 忽略大小写是否包含指定子串。static boolean是否包含任意空白字符(Character.isWhitespace(char))。static intcount(CharSequence str, char c) 统计字符在字符串中出现的次数。static intcount(CharSequence str, CharSequence sub) 统计子串在字符串中出现的次数。static Stringdifference(CharSequence str1, CharSequence str2) 返回首个差异位置起的子串(参考 commons-langStringUtils.difference)。static StringemptyToDefault(CharSequence str, String defaultStr) 空字符串使用默认值替换。static StringemptyToNull(CharSequence str) 空串转null。static booleanendWith(CharSequence str, CharSequence suffix) 是否以指定后缀结尾。static booleanendWithAny(CharSequence str, CharSequence... suffixes) 是否以任一后缀结尾。static booleanendWithIgnoreCase(CharSequence str, CharSequence suffix) 忽略大小写是否以指定后缀结尾。static booleanequals(CharSequence str1, CharSequence str2) 比较两个字符串是否相等。static booleanequalsAny(CharSequence str, CharSequence... values) 判断字符串是否等于给定值中的任意一个。static booleanequalsAnyIgnoreCase(CharSequence str, CharSequence... values) 忽略大小写判断字符串是否等于给定值中的任意一个。static booleanequalsIgnoreCase(CharSequence str1, CharSequence str2) 忽略大小写比较两个字符串是否相等。static String后补位:字符串不足指定长度时在后面填充字符。static StringfillBefore(String str, int minLen, char padChar) 前补位:字符串不足指定长度时在前面填充字符。static StringfirstNonBlank(String... values) 返回第一个非空白字符串。static Stringformat(CharSequence template, Object... args) 字符串格式化,使用一对花括号作为占位符,如format("你好,{}", "世界")。static StringgetCommonPrefix(CharSequence... strs) 多个字符串的公共前缀(参考 commons-langStringUtils.getCommonPrefix)。static booleanhasBlank(CharSequence... strs) 多个字符串中是否存在空白。static booleanhasEmpty(CharSequence... strs) 多个字符串中是否存在空串(null或长度为 0)。static Stringhide(CharSequence str, int fromInclude, int toExclude, char maskChar) 掩码:将 [fromInclude, toExclude) 区间字符替换为掩码符。static intindexOf(CharSequence str, CharSequence search) 查找子串首次出现位置。static intindexOfAny(CharSequence str, CharSequence... searchStrs) 返回任一给定子串在原字符串中最早出现的位置。static intindexOfIgnoreCase(CharSequence str, CharSequence search, int from) 忽略大小写查找子串位置。static booleanisAllBlank(CharSequence... strs) 多个字符串是否全部为空白。static booleanisAllEmpty(CharSequence... strs) 判断字符串数组中是否全部为空。static booleanisAllNotBlank(CharSequence... strs) 判断字符串数组中是否全部非空白。static booleanisAlpha(CharSequence str) 是否为纯字母(仅含字母字符)。static boolean是否仅含字母或数字。static boolean是否全为可打印 ASCII 字符(码位 32–126,含空格)。static booleanisBlank(CharSequence str) 字符串是否为空白:null、空串或全为空白字符。static booleanisEmpty(CharSequence str) 字符串是否为空:null或长度为 0。static booleanisLowerCase(String str) 判断是否全部为小写字母(忽略非字母字符)。static booleanisNotBlank(CharSequence str) 字符串是否非空白。static booleanisNotEmpty(CharSequence str) 字符串是否非空。static booleanisNumeric(CharSequence str) 是否为数字字符串(允许正负号、小数点)。static boolean是否仅含数字或空格。static booleanisUpperCase(String str) 判断是否全部为大写字母(忽略非字母字符)。static booleanisWrap(CharSequence str, String prefix, String suffix) 判断字符串是否被指定前后缀包裹。static Stringjoin(CharSequence delimiter, Iterable<?> iterable) 使用分隔符拼接可迭代集合。static Stringjoin(CharSequence delimiter, Object... objs) 使用分隔符拼接对象数组。static intlastIndexOf(CharSequence str, CharSequence search) 查找子串末次出现位置。static intlastIndexOfAny(CharSequence str, CharSequence... searchStrs) 返回任一给定子串在原字符串中最后出现的位置(以子串结尾为参照)。static StringlowerFirst(CharSequence str) 首字母小写。static String限制字符串最大长度,超出截断并追加省略号。static String空白规范化:首尾去空白,内部连续空白(含换行制表)压缩为单个空格。static StringnullToEmpty(CharSequence str) null转空串。static StringpadAfter(CharSequence str, int minLength, char padChar) 字符串后补齐至指定长度。static StringpadPre(CharSequence str, int minLength, char padChar) 字符串前补齐至指定长度。static StringremoveAll(CharSequence str, CharSequence... values) 移除所有指定子串。static StringremovePrefix(CharSequence str, CharSequence prefix) 去除字符串开头的指定前缀。static StringremovePrefix(String str, String prefix) 去除指定前缀(无前缀则原样返回)。static StringremovePrefixIgnoreCase(CharSequence str, CharSequence prefix) 忽略大小写去除字符串开头的指定前缀。static StringremoveSuffix(CharSequence str, CharSequence suffix) 去除字符串结尾的指定后缀。static StringremoveSuffix(String str, String suffix) 去除指定后缀(无后缀则原样返回)。static StringremoveSuffixIgnoreCase(CharSequence str, CharSequence suffix) 忽略大小写去除字符串结尾的指定后缀。static StringremoveSurrounding(CharSequence str, CharSequence wrap) 当字符串以给定前后缀同时包裹时,一次性剥去首尾(前后缀相同字符串)。static StringremoveSurrounding(CharSequence str, CharSequence prefix, CharSequence suffix) 当字符串以给定前后缀同时包裹时,一次性剥去首尾。static StringremoveSurroundingIgnoreCase(CharSequence str, CharSequence wrap) 忽略大小写:当字符串以给定前后缀同时包裹时,一次性剥去首尾(前后缀相同字符串)。static StringremoveSurroundingIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix) 忽略大小写:当字符串以给定前后缀同时包裹时,一次性剥去首尾。static String移除字符串中的全部空白字符。static Stringrepeat(CharSequence str, int count) 重复字符串指定次数。static Stringreplace(CharSequence str, int beginIndex, int endIndex, CharSequence replacement) 替换字符串中指定区间(含 begin,不含 end)的内容。static Stringreplace(CharSequence str, CharSequence searchStr, CharSequence replacement) 替换字符串中的全部指定子串。static StringreplaceIgnoreCase(CharSequence str, CharSequence searchStr, CharSequence replacement) 忽略大小写替换所有匹配子串。static StringreplaceOnce(CharSequence str, CharSequence searchStr, CharSequence replacement) 仅替换第一次出现的搜索子串。static Stringreverse(CharSequence str) 反转字符串。static Stringrotate(CharSequence str, int shift) 循环移位:正数右移、负数左移(参考 commons-langStringUtils.rotate)。static String[]split(CharSequence str, char separator) 按单个字符分割字符串。static String[]split(CharSequence str, String separator) 按字符串分割(分隔符中的正则特殊字符会被转义)。static String[]splitPreserveAllTokens(CharSequence str, char separatorChar) 按分隔字符分割并保留全部空 token(连续分隔符、首尾分隔符产生的空串均保留)。static double[]splitToDoubleArray(String str) 分隔为 double 数组(空白分隔),非法元素按 0 处理。static int[]splitToIntArray(String str) 分隔为 int 数组(空白分隔),非法元素按 0 处理。static long[]splitToLongArray(String str) 分隔为 long 数组(空白分隔),非法元素按 0 处理。static String[]按分隔符拆分并去除每个元素首尾空白(保留空元素)。static booleanstartWith(CharSequence str, CharSequence prefix) 是否以指定前缀开头。static booleanstartWithAny(CharSequence str, CharSequence... prefixes) 是否以任一前缀开头。static booleanstartWithIgnoreCase(CharSequence str, CharSequence prefix) 忽略大小写是否以指定前缀开头。static String去除字符串首尾的空白字符。static String去除字符串首尾的指定字符。static String剥离结尾出现的字符集合字符(stripChars为null时剥离空白)。static StringstripStart(String str, String stripChars) 剥离开头出现的字符集合字符(stripChars为null时剥离空白)。static Stringsub(CharSequence str, int fromIndex) 从起始索引截取到字符串末尾。static Stringsub(CharSequence str, int fromIndex, int toIndex) 安全截取字符串,支持负索引(从末尾倒数)。static String截取指定分隔符之后的子串。static String截取指定分隔符之前的子串。static StringsubBetween(CharSequence str, CharSequence prefix, CharSequence suffix) 取两标记之间的内容(第一个匹配)。subBetweenAll(String str, String prefix, String suffix) 提取所有两两之间的内容。static String截取头部 n 个字符。static Stringsubstitute(String template, Map<String, ?> vars) static String模板变量替换(自定义定界符,默认转义符$),委托StrSubstitutor。static StringsubstringBetween(CharSequence str, CharSequence tag) 取标签之间的文本(单标签,首次匹配):形如<a>text</a>取text。static StringsubstringBetween(CharSequence str, CharSequence open, CharSequence close) 取开始与结束标签之间的文本(首次匹配)。static String截取尾部 n 个字符。static Stringsurround(CharSequence str, CharSequence wrap) 用指定包裹字符串将目标字符串首尾包裹(如用引号包裹)。static StringswapCase(CharSequence str) 大小写互换:大写转小写、小写转大写(参考 commons-langStringUtils.swapCase)。static String交换字符串中字母的大小写。static StringtoCamelCase(CharSequence name) 下划线命名转驼峰命名,如user_name→userName。static String对象转字符串,数组会展开输出,null转为"null"。static inttotalLength(CharSequence... strs) 计算多个字符串的总长度(null视为空串)。static String驼峰命名转下划线命名,如userName→user_name。static String转 Unicode 字符串(中文转 \uXXXX,ASCII 保留)。static Stringtrim(CharSequence str) 去除字符串首尾空白(使用String.trim()语义)。static String去除字符串尾部空白字符。static String去除字符串首部空白字符。static StringtrimToEmpty(CharSequence str) 去除首尾空白,结果为空白时返回空串。static StringtrimToNull(CharSequence str) 去除首尾空白,结果为空白时返回null。static Stringtruncate(CharSequence str, int maxWidth) 精确截断字符串:长度超过maxWidth时截取前maxWidth个字符(不附加省略号)。static StringunWrap(CharSequence str, CharSequence prefix, CharSequence suffix) 去除字符串包裹(仅当首尾实际匹配时去除,否则原样返回)。static StringupperFirst(CharSequence str) 首字母大写。static Stringwrap(CharSequence str, CharSequence prefix, CharSequence suffix) 使用指定前后缀包裹字符串。
-
Field Details
-
EMPTY
-
SPACE
-
-
Method Details
-
isEmpty
字符串是否为空:null或长度为 0。- Parameters:
str- 字符串- Returns:
- 是否为空
-
isNotEmpty
-
isBlank
字符串是否为空白:null、空串或全为空白字符。- Parameters:
str- 字符串- Returns:
- 是否空白
-
isNotBlank
-
hasBlank
多个字符串中是否存在空白。- Parameters:
strs- 字符串数组- Returns:
- 是否存在空白
-
isAllBlank
多个字符串是否全部为空白。- Parameters:
strs- 字符串数组- Returns:
- 是否全部空白
-
hasEmpty
多个字符串中是否存在空串(null或长度为 0)。- Parameters:
strs- 字符串数组- Returns:
- 是否存在空串
-
trim
去除字符串首尾空白(使用String.trim()语义)。- Parameters:
str- 字符串- Returns:
- 去空白后的字符串,
null时返回空串
-
trimToNull
去除首尾空白,结果为空白时返回null。- Parameters:
str- 字符串- Returns:
- 去空白后的字符串或
null
-
trimToEmpty
去除首尾空白,结果为空白时返回空串。- Parameters:
str- 字符串- Returns:
- 去空白后的字符串
-
cleanBlank
去除字符串中所有空白字符(包括空格、制表符、换行等)。- Parameters:
str- 字符串- Returns:
- 去除所有空白后的字符串
-
equals
比较两个字符串是否相等。- Parameters:
str1- 字符串 1str2- 字符串 2- Returns:
- 是否相等
-
equalsIgnoreCase
忽略大小写比较两个字符串是否相等。- Parameters:
str1- 字符串 1str2- 字符串 2- Returns:
- 是否相等
-
contains
是否包含指定子串。- Parameters:
str- 原字符串searchStr- 要查找的子串- Returns:
- 是否包含
-
containsIgnoreCase
忽略大小写是否包含指定子串。- Parameters:
str- 原字符串searchStr- 要查找的子串- Returns:
- 是否包含
-
containsAny
是否包含任一指定子串。- Parameters:
str- 原字符串searchStr- 要查找的子串数组- Returns:
- 是否包含任意一个
-
startWith
是否以指定前缀开头。- Parameters:
str- 字符串prefix- 前缀- Returns:
- 是否以该前缀开头
-
startWithIgnoreCase
忽略大小写是否以指定前缀开头。- Parameters:
str- 字符串prefix- 前缀- Returns:
- 是否以该前缀开头
-
endWith
是否以指定后缀结尾。- Parameters:
str- 字符串suffix- 后缀- Returns:
- 是否以该后缀结尾
-
endWithIgnoreCase
忽略大小写是否以指定后缀结尾。- Parameters:
str- 字符串suffix- 后缀- Returns:
- 是否以该后缀结尾
-
sub
安全截取字符串,支持负索引(从末尾倒数)。- Parameters:
str- 字符串fromIndex- 起始索引(含),负数表示从末尾倒数toIndex- 结束索引(不含),负数表示从末尾倒数- Returns:
- 截取结果,越界自动收敛
-
sub
从起始索引截取到字符串末尾。- Parameters:
str- 字符串fromIndex- 起始索引(含)- Returns:
- 截取结果
-
removePrefix
去除字符串开头的指定前缀。- Parameters:
str- 字符串prefix- 前缀- Returns:
- 去除前缀后的字符串
-
removePrefixIgnoreCase
忽略大小写去除字符串开头的指定前缀。- Parameters:
str- 字符串prefix- 前缀- Returns:
- 去除前缀后的字符串
-
removeSuffix
去除字符串结尾的指定后缀。- Parameters:
str- 字符串suffix- 后缀- Returns:
- 去除后缀后的字符串
-
removeSuffixIgnoreCase
忽略大小写去除字符串结尾的指定后缀。- Parameters:
str- 字符串suffix- 后缀- Returns:
- 去除后缀后的字符串
-
replace
替换字符串中的全部指定子串。- Parameters:
str- 原字符串searchStr- 被替换的子串replacement- 替换后的子串- Returns:
- 替换后的字符串
-
join
使用分隔符拼接对象数组。- Parameters:
delimiter- 分隔符objs- 对象数组- Returns:
- 拼接结果
-
join
使用分隔符拼接可迭代集合。- Parameters:
delimiter- 分隔符iterable- 可迭代集合- Returns:
- 拼接结果
-
format
字符串格式化,使用一对花括号作为占位符,如format("你好,{}", "世界")。- Parameters:
template- 模板args- 参数- Returns:
- 格式化结果
-
substitute
模板变量替换(默认前缀${}、后缀 {@code }、转义符$),委托StrSubstitutor。支持默认值语法
${key:-default};变量缺失保留占位符原样; 值为null渲染为空串;$${输出字面${。示例:
String text = StrUtil.substitute("hi ${name}, env=${env:-dev}", Map.of("name", "sure")); // => "hi sure, env=dev"- Parameters:
template- 模板文本;为 null 时返回 nullvars- 变量映射;为 null 时视为空映射- Returns:
- 替换结果
- Since:
- 1.19.0
-
substitute
模板变量替换(自定义定界符,默认转义符$),委托StrSubstitutor。示例:
String text = StrUtil.substitute("<%name%> 你好", Map.of("name", "sure"), "<%", "%>"); // => "sure 你好"- Parameters:
template- 模板文本;为 null 时返回 nullvars- 变量映射;为 null 时视为空映射prefix- 占位符前缀suffix- 占位符后缀- Returns:
- 替换结果
- Throws:
IllegalArgumentException- 前缀或后缀为 null 或空串时抛出- Since:
- 1.19.0
-
repeat
重复字符串指定次数。- Parameters:
str- 字符串count- 重复次数- Returns:
- 重复后的字符串
-
padPre
字符串前补齐至指定长度。- Parameters:
str- 字符串minLength- 最小长度padChar- 补齐字符- Returns:
- 补齐后的字符串
-
padAfter
字符串后补齐至指定长度。- Parameters:
str- 字符串minLength- 最小长度padChar- 补齐字符- Returns:
- 补齐后的字符串
-
toCamelCase
下划线命名转驼峰命名,如user_name→userName。- Parameters:
name- 下划线命名- Returns:
- 驼峰命名
-
toUnderlineCase
驼峰命名转下划线命名,如userName→user_name。- Parameters:
str- 驼峰命名- Returns:
- 下划线命名
-
upperFirst
-
lowerFirst
-
reverse
-
count
统计字符在字符串中出现的次数。- Parameters:
str- 字符串c- 字符- Returns:
- 出现次数
-
count
统计子串在字符串中出现的次数。- Parameters:
str- 字符串sub- 子串- Returns:
- 出现次数
-
split
按单个字符分割字符串。- Parameters:
str- 字符串separator- 分隔字符- Returns:
- 分割后的字符串数组
-
split
按字符串分割(分隔符中的正则特殊字符会被转义)。- Parameters:
str- 字符串separator- 分隔符- Returns:
- 分割后的字符串数组
-
toString
-
bytes
字符串转字节数组。- Parameters:
str- 字符串charset- 字符集- Returns:
- 字节数组
-
nullToEmpty
-
emptyToNull
空串转null。- Parameters:
str- 字符串- Returns:
null或原字符串
-
blankToDefault
空白字符串使用默认值替换。- Parameters:
str- 字符串defaultStr- 默认值- Returns:
- 非空白时返回原串,否则返回默认值
-
emptyToDefault
空字符串使用默认值替换。- Parameters:
str- 字符串defaultStr- 默认值- Returns:
- 非空时返回原串,否则返回默认值
-
hide
掩码:将 [fromInclude, toExclude) 区间字符替换为掩码符。- Parameters:
str- 原字符串fromInclude- 起始(含)toExclude- 结束(不含)maskChar- 掩码符- Returns:
- 掩码后字符串;null 返回 null
-
subBetween
取两标记之间的内容(第一个匹配)。- Parameters:
str- 原字符串prefix- 前标记suffix- 后标记- Returns:
- 中间内容;未匹配返回 null
-
isNumeric
是否为数字字符串(允许正负号、小数点)。- Parameters:
str- 字符串- Returns:
- 是否数字
-
removeAll
移除所有指定子串。- Parameters:
str- 原字符串values- 要移除的子串- Returns:
- 移除后字符串;null 返回 null
-
indexOf
查找子串首次出现位置。- Parameters:
str- 原字符串search- 子串- Returns:
- 位置;未找到或参数为空返回 -1
-
lastIndexOf
查找子串末次出现位置。- Parameters:
str- 原字符串search- 子串- Returns:
- 位置;未找到或参数为空返回 -1
-
startWithAny
是否以任一前缀开头。- Parameters:
str- 字符串prefixes- 前缀组- Returns:
- 是否命中任一
-
endWithAny
是否以任一后缀结尾。- Parameters:
str- 字符串suffixes- 后缀组- Returns:
- 是否命中任一
-
splitToIntArray
分隔为 int 数组(空白分隔),非法元素按 0 处理。- Parameters:
str- 数字字符串- Returns:
- int 数组
-
splitToLongArray
分隔为 long 数组(空白分隔),非法元素按 0 处理。- Parameters:
str- 数字字符串- Returns:
- long 数组
-
subBetweenAll
-
splitToDoubleArray
分隔为 double 数组(空白分隔),非法元素按 0 处理。- Parameters:
str- 数字字符串- Returns:
- double 数组
-
removePrefix
-
removeSuffix
-
subSuf
-
subPre
-
splitTrim
-
blankToDefault
-
isUpperCase
判断是否全部为大写字母(忽略非字母字符)。- Parameters:
str- 字符串- Returns:
- 是否全大写;空串返回 false
-
isLowerCase
判断是否全部为小写字母(忽略非字母字符)。- Parameters:
str- 字符串- Returns:
- 是否全小写;空串返回 false
-
fillBefore
-
fillAfter
-
toUnicode
-
maxLength
-
strip
-
strip
-
center
-
center
-
firstNonBlank
-
concat
-
swapCase
-
isAllNotBlank
判断字符串数组中是否全部非空白。- Parameters:
strs- 字符串数组- Returns:
- 是否全部非空白
-
isAllEmpty
判断字符串数组中是否全部为空。- Parameters:
strs- 字符串数组- Returns:
- 是否全部为空
-
replace
public static String replace(CharSequence str, int beginIndex, int endIndex, CharSequence replacement) 替换字符串中指定区间(含 begin,不含 end)的内容。- Parameters:
str- 字符串beginIndex- 起始索引(含)endIndex- 结束索引(不含)replacement- 替换串- Returns:
- 替换后的字符串
-
containsAnyIgnoreCase
忽略大小写判断字符串是否包含任意给定子串。- Parameters:
str- 字符串searchStrs- 子串列表- Returns:
- 是否包含任意一个
-
replaceIgnoreCase
public static String replaceIgnoreCase(CharSequence str, CharSequence searchStr, CharSequence replacement) 忽略大小写替换所有匹配子串。- Parameters:
str- 字符串searchStr- 查找子串replacement- 替换串- Returns:
- 替换后的字符串
-
isWrap
判断字符串是否被指定前后缀包裹。例如
isWrap("[abc]", "[", "]")返回true。- Parameters:
str- 字符串prefix- 前缀suffix- 后缀- Returns:
- 是否被包裹
-
wrap
使用指定前后缀包裹字符串。- Parameters:
str- 字符串prefix- 前缀suffix- 后缀- Returns:
- 包裹后的字符串;
str为null时返回null
-
unWrap
去除字符串包裹(仅当首尾实际匹配时去除,否则原样返回)。- Parameters:
str- 字符串prefix- 前缀suffix- 后缀- Returns:
- 去除包裹后的字符串;
str为null时返回null
-
equalsAny
判断字符串是否等于给定值中的任意一个。- Parameters:
str- 字符串values- 候选值- Returns:
- 是否等于任意一个
-
equalsAnyIgnoreCase
忽略大小写判断字符串是否等于给定值中的任意一个。- Parameters:
str- 字符串values- 候选值- Returns:
- 是否等于任意一个
-
totalLength
计算多个字符串的总长度(null视为空串)。- Parameters:
strs- 字符串数组- Returns:
- 总长度
-
subBefore
-
subAfter
-
trimStart
-
trimEnd
-
surround
用指定包裹字符串将目标字符串首尾包裹(如用引号包裹)。- Parameters:
str- 目标字符串wrap- 包裹字符串- Returns:
- 包裹后的字符串;
str为null返回null - Since:
- 1.0.1
-
abbreviate
截断字符串:超过 maxWidth 时保留前 maxWidth-3 个字符并以...结尾。- Parameters:
str- 原字符串maxWidth- 最大宽度,小于 4 视为 4- Returns:
- 截断后的字符串;str 为 null 返回 null
-
abbreviate
截断字符串:从指定偏移开始,超过 maxWidth 时以...结尾。- Parameters:
str- 原字符串offset- 起始偏移(越界自动归位)maxWidth- 最大宽度,小于 4 视为 4- Returns:
- 截断后的字符串;str 为 null 返回 null
-
abbreviateMiddle
中间省略:保留首尾字符,中间以指定省略串连接(参考 commons-langStringUtils.abbreviateMiddle)。- Parameters:
str- 原字符串middle- 省略串(如"...")length- 目标总长度(含省略串),小于 middle 长度时直接返回 middle- Returns:
- 中间省略后的字符串;str 为 null 返回 null
-
substringBetween
取标签之间的文本(单标签,首次匹配):形如<a>text</a>取text。- Parameters:
str- 原字符串tag- 标签文本- Returns:
- 标签间文本;无匹配返回 null
-
substringBetween
取开始与结束标签之间的文本(首次匹配)。- Parameters:
str- 原字符串open- 开始标签close- 结束标签- Returns:
- 标签间文本;无匹配返回 null
-
difference
返回首个差异位置起的子串(参考 commons-langStringUtils.difference)。- Parameters:
str1- 字符串一str2- 字符串二- Returns:
- 从首个不同字符到 str2 末尾的子串;str1 为 null 返回 str2
-
indexOfIgnoreCase
忽略大小写查找子串位置。- Parameters:
str- 原字符串search- 查找目标from- 起始索引(负数按 0)- Returns:
- 首次命中的索引;str 为 null 返回 -1
-
swapCase
大小写互换:大写转小写、小写转大写(参考 commons-langStringUtils.swapCase)。- Parameters:
str- 原字符串- Returns:
- 互换后的字符串;str 为 null 返回 null
-
normalizeSpace
空白规范化:首尾去空白,内部连续空白(含换行制表)压缩为单个空格。- Parameters:
str- 原字符串- Returns:
- 规范化结果;str 为 null 返回 null
-
getCommonPrefix
多个字符串的公共前缀(参考 commons-langStringUtils.getCommonPrefix)。- Parameters:
strs- 字符串数组- Returns:
- 公共前缀;数组为空或含 null 返回空串
-
rotate
循环移位:正数右移、负数左移(参考 commons-langStringUtils.rotate)。- Parameters:
str- 原字符串shift- 移位位数(可为负)- Returns:
- 移位后的字符串;str 为 null 返回 null
-
isAlpha
是否为纯字母(仅含字母字符)。示例:
StrUtil.isAlpha("abc") -> true,StrUtil.isAlpha("a1") -> false。- Parameters:
str- 字符串,可为null- Returns:
- 全为字母返回
true;null或空串返回false
-
isAlphanumeric
是否仅含字母或数字。示例:
StrUtil.isAlphanumeric("abc123") -> true。- Parameters:
str- 字符串,可为null- Returns:
- 全为字母或数字返回
true;null或空串返回false
-
isNumericSpace
是否仅含数字或空格。示例:
StrUtil.isNumericSpace("12 345") -> true。- Parameters:
str- 字符串,可为null- Returns:
- 全为数字或空格返回
true;null或空串返回false
-
isAsciiPrintable
是否全为可打印 ASCII 字符(码位 32–126,含空格)。示例:
StrUtil.isAsciiPrintable("Hello!") -> true。- Parameters:
str- 字符串,可为null- Returns:
- 全为可打印 ASCII 返回
true;null或空串返回false
-
containsWhitespace
是否包含任意空白字符(Character.isWhitespace(char))。示例:
StrUtil.containsWhitespace("a b") -> true。- Parameters:
str- 字符串,可为null- Returns:
- 含空白返回
true;null或空串返回false
-
removeWhitespace
移除字符串中的全部空白字符。示例:
StrUtil.removeWhitespace(" a b ") -> "ab"。- Parameters:
str- 字符串,可为null- Returns:
- 去空白后的字符串;
null返回null
-
indexOfAny
返回任一给定子串在原字符串中最早出现的位置。示例:
StrUtil.indexOfAny("hello world", "world", "lo") -> 3。- Parameters:
str- 原字符串,可为nullsearchStrs- 待查找子串集合- Returns:
- 最早出现位置;未找到或参数为
null返回 -1
-
lastIndexOfAny
返回任一给定子串在原字符串中最后出现的位置(以子串结尾为参照)。示例:
StrUtil.lastIndexOfAny("hello lo", "lo", "ell") -> 7。- Parameters:
str- 原字符串,可为nullsearchStrs- 待查找子串集合- Returns:
- 最后出现位置;未找到或参数为
null返回 -1
-
replaceOnce
public static String replaceOnce(CharSequence str, CharSequence searchStr, CharSequence replacement) 仅替换第一次出现的搜索子串。示例:
StrUtil.replaceOnce("aa-bb-cc", "-", "+") -> "aa+bb-cc"。- Parameters:
str- 原字符串,可为nullsearchStr- 搜索子串,可为nullreplacement- 替换子串,可为null- Returns:
- 替换结果;
str为null返回null
-
removeSurrounding
当字符串以给定前后缀同时包裹时,一次性剥去首尾(前后缀相同字符串)。示例:
StrUtil.removeSurrounding("\"abc\"", "\"") -> "abc"。- Parameters:
str- 字符串,可为nullwrap- 同时作为前缀与后缀的字符串- Returns:
- 剥去包裹后的字符串;
null返回null
-
removeSurrounding
当字符串以给定前后缀同时包裹时,一次性剥去首尾。示例:
StrUtil.removeSurrounding("[abc]", "[", "]") -> "abc"。- Parameters:
str- 字符串,可为nullprefix- 前缀suffix- 后缀- Returns:
- 剥去包裹后的字符串;
str为null返回null
-
removeSurroundingIgnoreCase
忽略大小写:当字符串以给定前后缀同时包裹时,一次性剥去首尾(前后缀相同字符串)。- Parameters:
str- 字符串,可为nullwrap- 同时作为前缀与后缀的字符串- Returns:
- 剥去包裹后的字符串;
null返回null
-
removeSurroundingIgnoreCase
public static String removeSurroundingIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix) 忽略大小写:当字符串以给定前后缀同时包裹时,一次性剥去首尾。示例:
StrUtil.removeSurroundingIgnoreCase("[ABC]", "[", "]") -> "ABC"。- Parameters:
str- 字符串,可为nullprefix- 前缀suffix- 后缀- Returns:
- 剥去包裹后的字符串;
str为null返回null
-
truncate
精确截断字符串:长度超过maxWidth时截取前maxWidth个字符(不附加省略号)。语义对齐 Commons
StringUtils.truncate,与 HutoolmaxLength(保留省略号)不同: 示例:StrUtil.truncate("hello world", 5) -> "hello"。- Parameters:
str- 字符串,可为nullmaxWidth- 最大长度,负数抛IllegalArgumentException- Returns:
- 截断后的字符串;
null返回null
-
splitPreserveAllTokens
按分隔字符分割并保留全部空 token(连续分隔符、首尾分隔符产生的空串均保留)。示例:
StrUtil.splitPreserveAllTokens(",a,,b,", ',') -> ["", "a", "", "b", ""]。- Parameters:
str- 字符串,可为nullseparatorChar- 分隔字符- Returns:
- 分割结果数组;
null返回null
-
stripStart
-
stripEnd
-