public class CharSequenceUtil extends StrValidator
CharSequence
相关工具类封装,包括但不限于:
需要注意的是,strip、trim、wrap(unWrap)的策略不同:
Modifier and Type | Field and Description |
---|---|
static int |
INDEX_NOT_FOUND
未找到的的位置表示,用-1表示
|
EMPTY, NULL
Constructor and Description |
---|
CharSequenceUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
addPrefixIfNot(CharSequence str,
CharSequence prefix)
如果给定字符串不是以prefix开头的,在开头补充 prefix
|
static String |
addSuffixIfNot(CharSequence str,
CharSequence suffix)
如果给定字符串不是以suffix结尾的,在尾部补充 suffix
|
static String |
appendIfMissing(CharSequence str,
CharSequence suffix,
boolean ignoreCase,
CharSequence... testSuffixes)
如果给定字符串不是以给定的一个或多个字符串为结尾,则在尾部添加结尾字符串
|
static String |
appendIfMissing(CharSequence str,
CharSequence suffix,
CharSequence... suffixes)
如果给定字符串不是以给定的一个或多个字符串为结尾,则在尾部添加结尾字符串
不忽略大小写 |
static String |
appendIfMissingIgnoreCase(CharSequence str,
CharSequence suffix,
CharSequence... suffixes)
如果给定字符串不是以给定的一个或多个字符串为结尾,则在尾部添加结尾字符串
忽略大小写 |
static String |
brief(CharSequence str,
int maxLength)
将给定字符串,变成 "xxx...xxx" 形式的字符串
abcdefgh 9 -》 abcdefgh
abcdefgh 8 -》 abcdefgh
abcdefgh 7 -》 ab...gh
abcdefgh 6 -》 ab...h
abcdefgh 5 -》 a...h
abcdefgh 4 -》 a..h
abcdefgh 3 -》 a.h
abcdefgh 2 -》 a.
|
static StringBuilder |
builder(CharSequence... strs)
创建StringBuilder对象
|
static StringBuilder |
builder(CharSequence str)
创建StringBuilder对象
如果对象本身为 StringBuilder ,直接返回,否则新建 |
static int |
byteLength(CharSequence cs,
Charset charset)
给定字符串转为bytes后的byte数(byte长度)
|
static String |
center(CharSequence str,
int size)
居中字符串,两边补充指定字符串,如果指定长度小于字符串,则返回原字符串
center(null, *) = null
center("", 4) = " "
center("ab", -1) = "ab"
center("ab", 4) = " ab "
center("abcd", 2) = "abcd"
center("a", 4) = " a "
|
static String |
center(CharSequence str,
int size,
char padChar)
居中字符串,两边补充指定字符串,如果指定长度小于字符串,则返回原字符串
center(null, *, *) = null
center("", 4, ' ') = " "
center("ab", -1, ' ') = "ab"
center("ab", 4, ' ') = " ab "
center("abcd", 2, ' ') = "abcd"
center("a", 4, ' ') = " a "
center("a", 4, 'y') = "yayy"
center("abc", 7, ' ') = " abc "
|
static String |
center(CharSequence str,
int size,
CharSequence padStr)
居中字符串,两边补充指定字符串,如果指定长度小于字符串,则返回原字符串
center(null, *, *) = null
center("", 4, " ") = " "
center("ab", -1, " ") = "ab"
center("ab", 4, " ") = " ab "
center("abcd", 2, " ") = "abcd"
center("a", 4, " ") = " a "
center("a", 4, "yz") = "yayz"
center("abc", 7, null) = " abc "
center("abc", 7, "") = " abc "
|
static String |
cleanBlank(CharSequence str)
清理空白字符
|
static int |
codeLength(CharSequence cs)
获取字符串的Unicode字符长度,如果为
null 返回0Unicode字符长度指实际Unicode字符个数,如emoji算一个字符 |
static CharSequence |
commonPrefix(CharSequence str1,
CharSequence str2)
获取两个字符串的公共前缀
commonPrefix("abb", "acc") // "a"
|
static CharSequence |
commonSuffix(CharSequence str1,
CharSequence str2)
获取两个字符串的公共后缀
commonSuffix("aba", "cba") // "ba"
|
static int |
compare(CharSequence str1,
CharSequence str2,
boolean nullIsLess)
比较两个字符串,用于排序
compare(null, null, *) = 0
compare(null , "a", true) < 0
compare(null , "a", false) > 0
compare("a", null, true) > 0
compare("a", null, false) < 0
compare("abc", "abc", *) = 0
compare("a", "b", *) < 0
compare("b", "a", *) > 0
compare("a", "B", *) > 0
compare("ab", "abc", *) < 0
|
static int |
compareIgnoreCase(CharSequence str1,
CharSequence str2,
boolean nullIsLess)
比较两个字符串,用于排序,大小写不敏感
compareIgnoreCase(null, null, *) = 0
compareIgnoreCase(null , "a", true) < 0
compareIgnoreCase(null , "a", false) > 0
compareIgnoreCase("a", null, true) > 0
compareIgnoreCase("a", null, false) < 0
compareIgnoreCase("abc", "abc", *) = 0
compareIgnoreCase("abc", "ABC", *) = 0
compareIgnoreCase("a", "b", *) < 0
compareIgnoreCase("b", "a", *) > 0
compareIgnoreCase("a", "B", *) < 0
compareIgnoreCase("A", "b", *) < 0
compareIgnoreCase("ab", "abc", *) < 0
|
static int |
compareVersion(CharSequence version1,
CharSequence version2)
比较两个版本
null版本排在最小:即: compareVersion(null, "v1") < 0 compareVersion("v1", "v1") = 0 compareVersion(null, null) = 0 compareVersion("v1", null) > 0 compareVersion("1.0.0", "1.0.2") < 0 compareVersion("1.0.2", "1.0.2a") < 0 compareVersion("1.13.0", "1.12.1c") > 0 compareVersion("V0.0.20170102", "V0.0.20170101") > 0 |
static String |
concat(boolean isNullToEmpty,
CharSequence... strs)
连接多个字符串为一个
|
static boolean |
contains(CharSequence str,
char searchChar)
指定字符是否在字符串中出现过
|
static boolean |
contains(CharSequence str,
CharSequence searchStr)
指定字符串是否在字符串中出现过
|
static boolean |
containsAll(CharSequence str,
CharSequence... testChars)
检查指定字符串中是否含给定的所有字符串
|
static boolean |
containsAny(CharSequence str,
char... testChars)
查找指定字符串是否包含指定字符列表中的任意一个字符
|
static boolean |
containsAny(CharSequence str,
CharSequence... testStrs)
查找指定字符串是否包含指定字符串列表中的任意一个字符串
|
static boolean |
containsAnyIgnoreCase(CharSequence str,
CharSequence... testStrs)
查找指定字符串是否包含指定字符串列表中的任意一个字符串
忽略大小写 |
static boolean |
containsBlank(CharSequence str)
给定字符串是否包含空白符(空白符包括空格、制表符、全角空格和不间断空格)
如果给定字符串为null或者"",则返回false |
static boolean |
containsIgnoreCase(CharSequence str,
CharSequence testStr)
是否包含特定字符,忽略大小写,如果给定两个参数都为
null ,返回true |
static boolean |
containsOnly(CharSequence str,
char... testChars)
检查指定字符串中是否只包含给定的字符
这里的containsOnly并不是必须全部给定的testChars都需要有,而是一个子集。 |
static int |
count(CharSequence content,
char charForSearch)
统计指定内容中包含指定字符的数量
|
static int |
count(CharSequence content,
CharSequence strForSearch)
统计指定内容中包含指定字符串的数量
参数为 null 或者 "" 返回 0 . |
static String[] |
cut(CharSequence str,
int partLength)
将字符串切分为N等份
|
static <T extends CharSequence,V> |
defaultIfBlank(T str,
Function<T,V> handler,
Supplier<? extends V> defaultSupplier)
如果被检查对象为
null 或 "" 或 空白字符串时,返回默认值(由 defaultValueSupplier 提供);否则直接返回 |
static <T extends CharSequence> |
defaultIfBlank(T str,
Supplier<? extends T> defaultSupplier)
如果给定对象为
null 或者"" 返回原值, 否则返回自定义handler处理后的返回值 |
static <T extends CharSequence> |
defaultIfBlank(T str,
T defaultValue)
如果给定对象为
null 或者""或者空白符返回默认值
defaultIfBlank(null, null) = null
defaultIfBlank(null, "") = ""
defaultIfBlank("", "zz") = "zz"
defaultIfBlank(" ", "zz") = "zz"
defaultIfBlank("abc", *) = "abc"
|
static <T extends CharSequence,V> |
defaultIfEmpty(T str,
Function<T,V> handler,
Supplier<? extends V> defaultSupplier)
如果给定对象为
null 或者"" 返回defaultHandler处理的结果, 否则返回自定义handler处理后的返回值 |
static <T extends CharSequence> |
defaultIfEmpty(T str,
Supplier<? extends T> defaultSupplier)
如果给定对象为
null 或者"" 返回原值, 否则返回自定义handler处理后的返回值 |
static <T extends CharSequence> |
defaultIfEmpty(T str,
T defaultValue)
如果给定对象为
null 或者 "" 返回默认值
defaultIfEmpty(null, null) = null
defaultIfEmpty(null, "") = ""
defaultIfEmpty("", "zz") = "zz"
defaultIfEmpty(" ", "zz") = " "
defaultIfEmpty("abc", *) = "abc"
|
static <T extends CharSequence,R> |
defaultIfNull(T source,
Function<? super T,? extends R> handler,
Supplier<? extends R> defaultSupplier)
如果给定字符串不为
null 返回自定义handler处理后的结果,否则返回 Supplier.get() 提供的默认值 |
static <T extends CharSequence> |
defaultIfNull(T source,
Supplier<? extends T> defaultSupplier)
如果给定字符串不为
null 返回原值, 否则返回 Supplier.get() 提供的默认值 |
static <T extends CharSequence> |
defaultIfNull(T str,
T defaultValue)
如果给定字符串为
null 返回默认值
defaultIfNull(null, null); // = null
defaultIfNull(null, ""); // = ""
defaultIfNull(null, "zz"); // = "zz"
defaultIfNull("abc", *); // = "abc"
|
static CharSequence |
emptyIfNull(CharSequence str)
|
static boolean |
endWith(CharSequence str,
char c)
字符串是否以给定字符结尾
|
static boolean |
endWith(CharSequence str,
CharSequence suffix)
是否以指定字符串结尾
|
static boolean |
endWith(CharSequence str,
CharSequence suffix,
boolean ignoreCase)
是否以指定字符串结尾
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false |
static boolean |
endWith(CharSequence str,
CharSequence suffix,
boolean ignoreCase,
boolean ignoreEquals)
是否以指定字符串结尾
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false |
static boolean |
endWithAny(CharSequence str,
CharSequence... suffixes)
给定字符串是否以任何一个字符串结尾
给定字符串和数组为空都返回false |
static boolean |
endWithAnyIgnoreCase(CharSequence str,
CharSequence... suffixes)
给定字符串是否以任何一个字符串结尾(忽略大小写)
给定字符串和数组为空都返回false |
static boolean |
endWithIgnoreCase(CharSequence str,
CharSequence suffix)
是否以指定字符串结尾,忽略大小写
|
static boolean |
equals(CharSequence str1,
CharSequence str2)
比较两个字符串(大小写敏感)。
|
static boolean |
equals(CharSequence str1,
CharSequence str2,
boolean ignoreCase)
比较两个字符串是否相等,规则如下
str1和str2都为
null
忽略大小写使用String.equalsIgnoreCase(String) 判断相等
不忽略大小写使用String.contentEquals(CharSequence) 判断相等
|
static boolean |
equalsAny(CharSequence str1,
boolean ignoreCase,
CharSequence... strs)
给定字符串是否与提供的中任一字符串相同,相同则返回
true ,没有相同的返回false 如果参与比对的字符串列表为空,返回 false |
static boolean |
equalsAny(CharSequence str1,
CharSequence... strs)
给定字符串是否与提供的中任一字符串相同,相同则返回
true ,没有相同的返回false 如果参与比对的字符串列表为空,返回 false |
static boolean |
equalsAnyIgnoreCase(CharSequence str1,
CharSequence... strs)
给定字符串是否与提供的中任意一个字符串相同(忽略大小写),相同则返回
true ,没有相同的返回false 如果参与比对的字符串列表为空,返回 false |
static boolean |
equalsCharAt(CharSequence str,
int position,
char c)
字符串指定位置的字符是否与给定字符相同
如果字符串为null,返回false 如果给定的位置大于字符串长度,返回false 如果给定的位置小于0,返回false |
static boolean |
equalsIgnoreCase(CharSequence str1,
CharSequence str2)
比较两个字符串(大小写不敏感)。
|
static String |
filter(CharSequence str,
Predicate<Character> predicate)
过滤字符串
|
static <T extends CharSequence> |
firstNonBlank(T... strs)
返回第一个非blank 元素
|
static <T extends CharSequence> |
firstNonEmpty(T... strs)
返回第一个非empty 元素
|
static <T extends CharSequence> |
firstNonNull(T... strs)
返回第一个非
null 元素 |
static String |
fixLength(CharSequence str,
char fixedChar,
int length)
在给定字符串末尾填充指定字符,以达到给定长度
如果字符串本身的长度大于等于length,返回原字符串 |
static void |
forEach(CharSequence str,
boolean isCodePoint,
IntConsumer consumer)
遍历字符串的每个字符,并处理
|
static void |
forEach(CharSequence str,
Consumer<Character> consumer)
遍历字符串的每个字符,并处理
|
static String |
format(CharSequence template,
Object... params)
格式化文本, {} 表示占位符
此方法只是简单将占位符 {} 按照顺序替换为参数 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可 例: 通常使用:format("this is {} for {}", "a", "b") =》 this is a for b 转义{}: format("this is \\{} for {}", "a", "b") =》 this is {} for a 转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for b |
static String |
formatByBean(CharSequence template,
Object bean,
boolean ignoreNull)
格式化文本,使用 {varName} 占位
bean = User:{a: "aValue", b: "bValue"} format("{a} and {b}", bean) ---=》 aValue and bValue |
static String |
formatByMap(CharSequence template,
Map<?,?> map)
格式化文本,使用 {varName} 占位
map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue |
static String |
formatByMap(CharSequence template,
Map<?,?> map,
boolean ignoreNull)
格式化文本,使用 {varName} 占位
map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue |
static String |
getContainsStr(CharSequence str,
CharSequence... testStrs)
查找指定字符串是否包含指定字符串列表中的任意一个字符串,如果包含返回找到的第一个字符串
|
static String |
getContainsStrIgnoreCase(CharSequence str,
CharSequence... testStrs)
查找指定字符串是否包含指定字符串列表中的任意一个字符串,如果包含返回找到的第一个字符串
忽略大小写 |
static String |
hide(CharSequence str,
int startInclude,
int endExclude)
替换指定字符串的指定区间内字符为"*"
俗称:脱敏功能,后面其他功能,可以见:DesensitizedUtil(脱敏工具类)
hide(null,*,*)=null
hide("",0,*)=""
hide("jackduan@163.com",-1,4) ****duan@163.com
hide("jackduan@163.com",2,3) ja*kduan@163.com
hide("jackduan@163.com",3,2) jackduan@163.com
hide("jackduan@163.com",16,16) jackduan@163.com
hide("jackduan@163.com",16,17) jackduan@163.com
|
static String |
indexedFormat(CharSequence pattern,
Object... arguments)
有序的格式化文本,使用{number}做为占位符
通常使用:format("this is {0} for {1}", "a", "b") =》 this is a for b |
static int |
indexOf(CharSequence str,
char searchChar)
指定范围内查找指定字符
|
static int |
indexOf(CharSequence str,
char searchChar,
int start)
指定范围内查找指定字符
|
static int |
indexOf(CharSequence text,
char searchChar,
int start,
int end)
指定范围内查找指定字符
|
static int |
indexOf(CharSequence text,
CharSequence searchStr,
int from,
boolean ignoreCase)
指定范围内查找字符串
|
static int |
indexOf(CharSequence text,
Predicate<Character> matcher,
int start,
int end)
指定范围内查找指定字符
|
static int |
indexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
指定范围内查找字符串,忽略大小写
indexOfIgnoreCase(null, *, *) = -1 indexOfIgnoreCase(*, null, *) = -1 indexOfIgnoreCase("", "", 0) = 0 indexOfIgnoreCase("aabaabaa", "A", 0) = 0 indexOfIgnoreCase("aabaabaa", "B", 0) = 2 indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 indexOfIgnoreCase("aabaabaa", "B", 3) = 5 indexOfIgnoreCase("aabaabaa", "B", 9) = -1 indexOfIgnoreCase("aabaabaa", "B", -1) = 2 indexOfIgnoreCase("aabaabaa", "", 2) = 2 indexOfIgnoreCase("abc", "", 9) = -1 |
static int |
indexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int fromIndex)
指定范围内查找字符串
indexOfIgnoreCase(null, *, *) = -1
indexOfIgnoreCase(*, null, *) = -1
indexOfIgnoreCase("", "", 0) = 0
indexOfIgnoreCase("aabaabaa", "A", 0) = 0
indexOfIgnoreCase("aabaabaa", "B", 0) = 2
indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
indexOfIgnoreCase("aabaabaa", "B", 3) = 5
indexOfIgnoreCase("aabaabaa", "B", 9) = -1
indexOfIgnoreCase("aabaabaa", "B", -1) = 2
indexOfIgnoreCase("aabaabaa", "", 2) = 2
indexOfIgnoreCase("abc", "", 9) = -1
|
static boolean |
isCharEquals(CharSequence str)
检查给定字符串的所有字符是否都一样
|
static boolean |
isLowerCase(CharSequence str)
给定字符串中的字母是否全部为小写,判断依据如下:
1.
|
static boolean |
isNumeric(CharSequence str)
检查字符串是否都为数字组成
|
static boolean |
isSubEquals(CharSequence str1,
int offset1,
CharSequence str2,
boolean ignoreCase)
截取第一个字串的部分字符,与第二个字符串比较(长度一致),判断截取的子串是否相同
任意一个字符串为null返回false |
static boolean |
isSubEquals(CharSequence str1,
int offset1,
CharSequence str2,
int offset2,
int length,
boolean ignoreCase)
截取两个字符串的不同部分(长度一致),判断截取的子串是否相同
任意一个字符串为null返回false |
static boolean |
isUpperCase(CharSequence str)
给定字符串中的字母是否全部为大写,判断依据如下:
1.
|
static boolean |
isWrap(CharSequence str,
char wrapper)
指定字符串是否被同一字符包装(前后都有这些字符串)
|
static boolean |
isWrap(CharSequence str,
char prefixChar,
char suffixChar)
指定字符串是否被包装
|
static boolean |
isWrap(CharSequence str,
CharSequence prefix,
CharSequence suffix)
指定字符串是否被包装
|
static boolean |
isWrap(CharSequence str,
String wrapper)
指定字符串是否被同一字符包装(前后都有这些字符串)
|
static <T> String |
join(CharSequence conjunction,
Iterable<T> iterable)
以 conjunction 为分隔符将多个对象转换为字符串
|
static String |
join(CharSequence conjunction,
Object... objs)
以 conjunction 为分隔符将多个对象转换为字符串
|
static int |
lastIndexOf(CharSequence text,
CharSequence searchStr,
int from,
boolean ignoreCase)
指定范围内查找字符串
fromIndex 为搜索起始位置,从后往前计数 |
static int |
lastIndexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
指定范围内查找字符串,忽略大小写
|
static int |
lastIndexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int fromIndex)
指定范围内查找字符串,忽略大小写
fromIndex 为搜索起始位置,从后往前计数 |
static int |
length(CharSequence cs)
获取字符串的长度,如果为null返回0
|
static String |
limitByteLength(CharSequence str,
Charset charset,
int maxBytesLength,
int factor,
boolean appendDots)
截断字符串,使用其按照指定编码为字节后不超过maxBytes长度
此方法用于截取总bytes数不超过指定长度,如果字符出没有超出原样输出,如果超出了,则截取掉超出部分,并可选添加..., 但是添加“...”后总长度也不超过限制长度。 |
static String |
limitByteLengthUtf8(CharSequence str,
int maxBytesLength,
boolean appendDots)
截断字符串,使用UTF8编码为字节后不超过maxBytes长度
|
static String |
limitLength(CharSequence string,
int length)
限制字符串长度,如果超过指定长度,截取指定长度并在末尾加"..."
|
static String |
lowerAt(CharSequence str,
int index)
小写对应下标字母
例如: str = NAME,index = 1, return NaME |
static String |
lowerFirst(CharSequence str)
小写首字母
例如:str = Name, return name |
static String |
move(CharSequence str,
int startInclude,
int endExclude,
int moveLength)
循环位移指定位置的字符串为指定距离
当moveLength大于0向右位移,小于0向左位移,0不位移 当moveLength大于字符串长度时采取循环位移策略,即位移到头后从头(尾)位移,例如长度为10,位移13则表示位移3 |
static String |
normalize(CharSequence str)
对字符串归一化处理,如 "Á" 可以使用 "u00C1"或 "u0041u0301"表示,实际测试中两个字符串并不equals
因此使用此方法归一为一种表示形式,默认按照W3C通常建议的,在NFC中交换文本。 |
static <T extends CharSequence> |
nullIfBlank(T str)
当给定字符串为空字符串时,转换为
null |
static <T extends CharSequence> |
nullIfEmpty(T str)
当给定字符串为空字符串时,转换为
null |
static int |
ordinalIndexOf(CharSequence str,
CharSequence searchStr,
int ordinal)
返回字符串 searchStr 在字符串 str 中第 ordinal 次出现的位置。
|
static String |
padAfter(CharSequence str,
int length,
char padChar)
补充字符串以满足最小长度,如果提供的字符串大于指定长度,截断之
padAfter(null, *, *);//null
padAfter("1", 3, '0');//"100"
padAfter("123", 2, '0');//"23"
padAfter("123", -1, '0')//"" 空串
|
static String |
padAfter(CharSequence str,
int length,
CharSequence padStr)
补充字符串以满足最小长度
padAfter(null, *, *);//null
padAfter("1", 3, "ABC");//"1AB"
padAfter("123", 2, "ABC");//"23"
|
static String |
padPre(CharSequence str,
int length,
char padChar)
补充字符串以满足最小长度,如果提供的字符串大于指定长度,截断之
同:leftPad (org.apache.commons.lang3.leftPad)
padPre(null, *, *);//null
padPre("1", 3, '0');//"001"
padPre("123", 2, '0');//"12"
|
static String |
padPre(CharSequence str,
int length,
CharSequence padStr)
补充字符串以满足指定长度,如果提供的字符串大于指定长度,截断之
同:leftPad (org.apache.commons.lang3.leftPad)
padPre(null, *, *);//null
padPre("1", 3, "ABC");//"AB1"
padPre("123", 2, "ABC");//"12"
padPre("1039", -1, "0");//"103"
|
static String |
prependIfMissing(CharSequence str,
CharSequence prefix,
boolean ignoreCase,
CharSequence... prefixes)
如果给定字符串不是以给定的一个或多个字符串为开头,则在首部添加起始字符串
|
static String |
prependIfMissing(CharSequence str,
CharSequence prefix,
CharSequence... prefixes)
如果给定字符串不是以给定的一个或多个字符串为开头,则在前面添加起始字符串
不忽略大小写 |
static String |
prependIfMissingIgnoreCase(CharSequence str,
CharSequence prefix,
CharSequence... prefixes)
如果给定字符串不是以给定的一个或多个字符串为开头,则在首部添加起始字符串
忽略大小写 |
static String |
removeAll(CharSequence str,
char... chars)
去除字符串中指定的多个字符,如有多个则全部去除
|
static String |
removeAll(CharSequence str,
CharSequence... strsToRemove)
移除字符串中所有给定字符串,当某个字符串出现多次,则全部移除
例:removeAny("aa-bb-cc-dd", "a", "b") =》 --cc-dd |
static String |
removeAll(CharSequence str,
CharSequence strToRemove)
移除字符串中所有给定字符串
例:removeAll("aa-bb-cc-dd", "-") =》 aabbccdd |
static String |
removeAllLineBreaks(CharSequence str)
去除所有换行符,包括:
1.
|
static String |
removeAllPrefix(CharSequence str,
CharSequence prefix)
去掉指定所有前缀,如:
str=abcdef, prefix=ab => return cdef
str=ababcdef, prefix=ab => return cdef
str=ababcdef, prefix="" => return ababcdef
str=ababcdef, prefix=null => return ababcdef
|
static String |
removeAllSuffix(CharSequence str,
CharSequence suffix)
去掉指定所有后缀,如:
str=11abab, suffix=ab => return 11
str=11ab, suffix=ab => return 11
str=11ab, suffix="" => return 11ab
str=11ab, suffix=null => return 11ab
|
static String |
removePreAndLowerFirst(CharSequence str,
CharSequence prefix)
去掉首部指定长度的字符串并将剩余字符串首字母小写
例如:str=setName, prefix=set =》 return name |
static String |
removePreAndLowerFirst(CharSequence str,
int preLength)
去掉首部指定长度的字符串并将剩余字符串首字母小写
例如:str=setName, preLength=3 =》 return name |
static String |
removePrefix(CharSequence str,
CharSequence prefix)
去掉指定前缀
|
static String |
removePrefix(CharSequence str,
CharSequence prefix,
boolean ignoreCase)
去掉指定前缀
|
static String |
removePrefixIgnoreCase(CharSequence str,
CharSequence prefix)
忽略大小写去掉指定前缀
|
static String |
removeSufAndLowerFirst(CharSequence str,
CharSequence suffix)
去掉指定后缀,并小写首字母
|
static String |
removeSuffix(CharSequence str,
CharSequence suffix)
去掉指定后缀
|
static String |
removeSuffixIgnoreCase(CharSequence str,
CharSequence suffix)
忽略大小写去掉指定后缀
|
static String |
repeat(char c,
int count)
重复某个字符
repeat('e', 0) = ""
repeat('e', 3) = "eee"
repeat('e', -2) = ""
|
static String |
repeat(CharSequence str,
int count)
重复某个字符串
|
static String |
repeatAndJoin(CharSequence str,
int count,
CharSequence delimiter)
重复某个字符串并通过分界符连接
repeatAndJoin("?"
|
static String |
repeatByLength(CharSequence str,
int padLen)
重复某个字符串到指定长度
如果指定长度非指定字符串的整数倍,截断到固定长度
如果指定长度小于字符串本身的长度,截断之
|
static String |
replace(CharSequence str,
CharSequence searchStr,
CharSequence replacement)
替换字符串中的指定字符串
|
static String |
replace(CharSequence str,
CharSequence searchStr,
CharSequence replacement,
boolean ignoreCase)
替换字符串中的指定字符串
|
static String |
replace(CharSequence str,
int fromIndex,
CharSequence searchStr,
CharSequence replacement,
boolean ignoreCase)
替换字符串中的指定字符串
如果指定字符串出现多次,则全部替换 |
static String |
replace(CharSequence str,
Pattern pattern,
SerFunction<Matcher,String> replaceFun)
替换所有正则匹配的文本,并使用自定义函数决定如何替换
replaceFun可以提取出匹配到的内容的不同部分,然后经过重新处理、组装变成新的内容放回原位。 |
static String |
replace(CharSequence str,
String regex,
SerFunction<Matcher,String> replaceFun)
替换所有正则匹配的文本,并使用自定义函数决定如何替换
|
static String |
replaceAt(CharSequence str,
int index,
UnaryOperator<Character> operator)
按照给定逻辑替换指定位置的字符,如字符大小写转换等
|
static String |
replaceByCodePoint(CharSequence str,
int beginInclude,
int endExclude,
char replacedChar)
|
static String |
replaceByCodePoint(CharSequence str,
int beginInclude,
int endExclude,
CharSequence replacedStr)
替换指定字符串的指定区间内字符为指定字符串,字符串只重复一次
此方法使用 CharSequence.codePoints() 完成拆分替换 |
static String |
replaceChars(CharSequence str,
char[] chars,
CharSequence replacedStr)
替换字符字符数组中所有的字符为replacedStr
|
static String |
replaceChars(CharSequence str,
String chars,
CharSequence replacedStr)
替换字符字符数组中所有的字符为replacedStr
提供的chars为所有需要被替换的字符,例如:"\r\n",则"\r"和"\n"都会被替换,哪怕他们单独存在 |
static String |
replaceFirst(CharSequence str,
CharSequence searchStr,
CharSequence replacedStr,
boolean ignoreCase)
替换字符串中第一个指定字符串
|
static String |
replaceIgnoreCase(CharSequence str,
CharSequence searchStr,
CharSequence replacement)
替换字符串中的指定字符串,忽略大小写
|
static String |
replaceLast(CharSequence str,
CharSequence searchStr,
CharSequence replacedStr,
boolean ignoreCase)
替换字符串中最后一个指定字符串
|
static boolean |
startWith(CharSequence str,
char c)
字符串是否以给定字符开始
|
static boolean |
startWith(CharSequence str,
CharSequence prefix)
是否以指定字符串开头
|
static boolean |
startWith(CharSequence str,
CharSequence prefix,
boolean ignoreCase)
是否以指定字符串开头
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false |
static boolean |
startWith(CharSequence str,
CharSequence prefix,
boolean ignoreCase,
boolean ignoreEquals)
是否以指定字符串开头
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false CharSequenceUtil.startWith("123", "123", false, true); -- false CharSequenceUtil.startWith("ABCDEF", "abc", true, true); -- true CharSequenceUtil.startWith("abc", "abc", true, true); -- false |
static boolean |
startWithAny(CharSequence str,
CharSequence... prefixes)
给定字符串是否以任何一个字符串开始
给定字符串和数组为空都返回false |
static boolean |
startWithAnyIgnoreCase(CharSequence str,
CharSequence... prefixes)
给定字符串是否以任何一个字符串开始(忽略大小写)
给定字符串和数组为空都返回false |
static boolean |
startWithIgnoreCase(CharSequence str,
CharSequence prefix)
是否以指定字符串开头,忽略大小写
|
static boolean |
startWithIgnoreEquals(CharSequence str,
CharSequence prefix)
是否以指定字符串开头,忽略相等字符串的情况
|
static String |
strip(CharSequence str,
CharSequence prefixOrSuffix)
去除两边的指定字符串
"aaa_STRIPPED_bbb", "a" -> "aa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "" -> "aaa_STRIPPED_bbb"
|
static String |
strip(CharSequence str,
CharSequence prefix,
CharSequence suffix)
去除两边的指定字符串
两边字符如果存在,则去除,不存在不做处理
"aaa_STRIPPED_bbb", "a", "b" -> "aa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", "a", "" -> "aa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "aa_STRIPPED_bbb"
"a", "a", "a" -> ""
|
static String |
strip(CharSequence str,
CharSequence prefix,
CharSequence suffix,
boolean ignoreCase)
去除两边的指定字符串
两边字符如果存在,则去除,不存在不做处理
"aaa_STRIPPED_bbb", "a", "b" -> "aa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", "a", "" -> "aa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "aa_STRIPPED_bbb"
"a", "a", "a" -> ""
|
static String |
stripAll(CharSequence str,
CharSequence prefixOrSuffix)
去除两边所有的指定字符串
"aaa_STRIPPED_bbb", "a" -> "_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", "b" -> "_STRIPPED_"
"aaa_STRIPPED_bbb", "" -> "aaa_STRIPPED_bbb"
|
static String |
stripAll(CharSequence str,
CharSequence prefix,
CharSequence suffix)
去除两边所有的指定字符串
"aaa_STRIPPED_bbb", "a", "b" -> "_STRIPPED_"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", "a", "" -> "_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "_STRIPPED_bbb"
// special test
"aaaaaabbb", "aaa", null -> "bbb"
"aaaaaaabbb", "aa", null -> "abbb"
"aaaaaaaaa", "aaa", "aa" -> ""
"a", "a", "a" -> ""
|
static String |
stripAll(CharSequence str,
CharSequence prefix,
CharSequence suffix,
boolean ignoreCase)
去除两边所有的指定字符串
"aaa_STRIPPED_bbb", "a", "b" -> "_STRIPPED_"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", "a", "" -> "_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "_STRIPPED_bbb"
// special test
"aaaaaabbb", "aaa", null -> "bbb"
"aaaaaaabbb", "aa", null -> "abbb"
"aaaaaaaaa", "aaa", "aa" -> ""
"a", "a", "a" -> ""
|
static String |
stripIgnoreCase(CharSequence str,
CharSequence prefixOrSuffix)
去除两边的指定字符串,忽略大小写
|
static String |
stripIgnoreCase(CharSequence str,
CharSequence prefix,
CharSequence suffix)
去除两边的指定字符串,忽略大小写
|
static String |
sub(CharSequence str,
int fromIndexInclude,
int toIndexExclude)
改进JDK subString,规则如下:
index从0开始计算,最后一个字符为-1,即sub("hutool", 0, -1)得到"hutoo"
如果from和to位置一样,返回 ""
如果from或to为负数,则按照length从后向前数位置,如果绝对值大于字符串长度,则from归到0,to归到length
如果经过修正的index中from大于to,则互换from和to,如abcdefgh 2 3 =》 c,abcdefgh 2 -3 =》 cde
|
static String |
subAfter(CharSequence string,
char separator,
boolean isLastSeparator)
截取分隔字符串之后的字符串,不包括分隔字符串
如果给定的字符串为空串(null或""),返回原字符串 如果分隔字符串为空串(null或""),则返回空串,如果分隔字符串未找到,返回空串,举例如下: subAfter(null, *, false) = null subAfter("", *, false) = "" subAfter("abc", 'a', false) = "bc" subAfter("abcba", 'b', false) = "cba" subAfter("abc", 'c', false) = "" subAfter("abc", 'd', false) = "" |
static String |
subAfter(CharSequence string,
CharSequence separator,
boolean isLastSeparator)
截取分隔字符串之后的字符串,不包括分隔字符串
如果给定的字符串为空串(null或""),返回原字符串 如果分隔字符串为空串(null或""),则返回空串,如果分隔字符串未找到,返回空串,举例如下: subAfter(null, *, false) = null subAfter("", *, false) = "" subAfter(*, null, false) = "" subAfter("abc", "a", false) = "bc" subAfter("abcba", "b", false) = "cba" subAfter("abc", "c", false) = "" subAfter("abc", "d", false) = "" subAfter("abc", "", false) = "abc" |
static String |
subBefore(CharSequence string,
char separator,
boolean isLastSeparator)
截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串 如果分隔字符串未找到,返回原字符串,举例如下: subBefore(null, *, false) = null subBefore("", *, false) = "" subBefore("abc", 'a', false) = "" subBefore("abcba", 'b', false) = "a" subBefore("abc", 'c', false) = "ab" subBefore("abc", 'd', false) = "abc" |
static String |
subBefore(CharSequence string,
CharSequence separator,
boolean isLastSeparator)
截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串 如果分隔字符串为空串"",则返回空串,如果分隔字符串未找到,返回原字符串,举例如下: subBefore(null, *, false) = null subBefore("", *, false) = "" subBefore("abc", "a", false) = "" subBefore("abcba", "b", false) = "a" subBefore("abc", "c", false) = "ab" subBefore("abc", "d", false) = "abc" subBefore("abc", "", false) = "" subBefore("abc", null, false) = "abc" |
static String |
subBetween(CharSequence str,
CharSequence beforeAndAfter)
截取指定字符串中间部分,不包括标识字符串
栗子: subBetween(null, *) = null subBetween("", "") = "" subBetween("", "tag") = null subBetween("tagabctag", null) = null subBetween("tagabctag", "") = "" subBetween("tagabctag", "tag") = "abc" |
static String |
subBetween(CharSequence str,
CharSequence before,
CharSequence after)
截取指定字符串中间部分,不包括标识字符串
栗子: subBetween("wx[b]yz", "[", "]") = "b" subBetween(null, *, *) = null subBetween(*, null, *) = null subBetween(*, *, null) = null subBetween("", "", "") = "" subBetween("", "", "]") = null subBetween("", "[", "]") = null subBetween("yabcz", "", "") = "" subBetween("yabcz", "y", "z") = "abc" subBetween("yabczyabcz", "y", "z") = "abc" |
static String[] |
subBetweenAll(CharSequence str,
CharSequence prefixAndSuffix)
截取指定字符串多段中间部分,不包括标识字符串
栗子: subBetweenAll(null, *) = [] subBetweenAll(*, null) = [] subBetweenAll(*, *) = [] subBetweenAll("", "") = [] subBetweenAll("", "#") = [] subBetweenAll("gotanks", "") = [] subBetweenAll("#gotanks#", "#") = ["gotanks"] subBetweenAll("#hello# #world#!" |
static String[] |
subBetweenAll(CharSequence str,
CharSequence prefix,
CharSequence suffix)
截取指定字符串多段中间部分,不包括标识字符串
栗子: subBetweenAll("wx[b]y[z]", "[", "]") = ["b","z"] subBetweenAll(null, *, *) = [] subBetweenAll(*, null, *) = [] subBetweenAll(*, *, null) = [] subBetweenAll("", "", "") = [] subBetweenAll("", "", "]") = [] subBetweenAll("", "[", "]") = [] subBetweenAll("yabcz", "", "") = [] subBetweenAll("yabcz", "y", "z") = ["abc"] subBetweenAll("yabczyabcz", "y", "z") = ["abc","abc"] subBetweenAll("[yabc[zy]abcz]", "[", "]"); = ["zy"] 重叠时只截取内部, |
static String |
subByCodePoint(CharSequence str,
int fromIndex,
int toIndex)
通过CodePoint截取字符串,可以截断Emoji
|
static String |
subByLength(String input,
int fromIndex,
int length)
截取字符串,从指定位置开始,截取指定长度的字符串
当fromIndex为正数时,这个index指的是插空位置,如下: 0 1 2 3 4 A B C D 当fromIndex为负数时,为反向插空位置,其中-1表示最后一个字符之前的位置: -3 -2 -1 length A B C D |
static String |
subPre(CharSequence string,
int toIndexExclude)
切割指定位置之前部分的字符串
安全的subString,允许:string为null,允许string长度小于toIndexExclude长度
assertEquals(subPre(null, 3), null);
assertEquals(subPre("ab", 3), "ab");
assertEquals(subPre("abc", 3), "abc");
assertEquals(subPre("abcd", 3), "abc");
assertEquals(subPre("abcd", -3), "a");
assertEquals(subPre("ab", 3), "ab");
|
static String |
subPreGbk(CharSequence str,
int len,
boolean halfUp)
截取部分字符串,这里一个汉字的长度认为是2
可以自定义halfUp,如len为10,如果截取后最后一个字符是半个字符, true 表示保留,则长度是11,否则长度9 |
static String |
subPreGbk(CharSequence str,
int len,
CharSequence suffix)
截取部分字符串,这里一个汉字的长度认为是2
|
static String |
subSuf(CharSequence string,
int fromIndex)
切割指定位置之后部分的字符串,规则如下:
fromIndex为0或字符串为空,返回原字符串
fromIndex大于字符串本身的长度,返回""
fromIndex支持负数,-1表示length-1
|
static String |
subSufByLength(CharSequence string,
int length)
切割指定长度的后部分的字符串
subSufByLength("abcde", 3) = "cde"
subSufByLength("abcde", 0) = ""
subSufByLength("abcde", -5) = ""
subSufByLength("abcde", -1) = ""
subSufByLength("abcde", 5) = "abcde"
subSufByLength("abcde", 10) = "abcde"
subSufByLength(null, 3) = null
|
static String |
swapCase(String str)
切换给定字符串中的大小写。
|
static String |
toCamelCase(CharSequence name)
将下划线方式命名的字符串转换为驼峰式。
|
static String |
toCamelCase(CharSequence name,
char symbol)
将连接符方式命名的字符串转换为驼峰式。
|
static int[] |
toChars(CharSequence str,
boolean isCodePoint)
将字符串转换为字符数组
|
static String |
toString(Object obj)
调用对象的toString方法,null会返回“null”
|
static String |
toStringOrEmpty(Object obj)
|
static String |
toStringOrNull(Object obj)
调用对象的toString方法,
null 会返回null |
static String |
toSymbolCase(CharSequence str,
char symbol)
将驼峰式命名的字符串转换为使用符号连接方式。
|
static int |
totalLength(CharSequence... strs)
给定字符串数组的总长度
null字符长度定义为0 |
static String |
toUnderlineCase(CharSequence str)
将驼峰式命名的字符串转换为下划线方式。
|
static String |
trim(CharSequence str)
除去字符串头尾部的空白,如果字符串是
null ,依然返回null 。 |
static String |
trim(CharSequence str,
StrTrimer.TrimMode mode)
除去字符串头尾部的空白符,如果字符串是
null ,依然返回null 。 |
static String |
trim(CharSequence str,
StrTrimer.TrimMode mode,
Predicate<Character> predicate)
按照断言,除去字符串头尾部的断言为真的字符,如果字符串是
null ,依然返回null 。 |
static String |
trimPrefix(CharSequence str)
除去字符串头部的空白,如果字符串是
null ,则返回null 。 |
static String |
trimSuffix(CharSequence str)
除去字符串尾部的空白,如果字符串是
null ,则返回null 。 |
static String |
trimToEmpty(CharSequence str)
除去字符串头尾部的空白,如果字符串是
null ,返回"" 。 |
static String |
trimToNull(CharSequence str)
除去字符串头尾部的空白,如果字符串是
null 或者"",返回null 。 |
static String |
unWrap(CharSequence str,
char prefixAndSuffix)
去掉字符包装,如果未被包装则返回原字符串
|
static String |
unWrap(CharSequence str,
char prefix,
char suffix)
去掉字符包装,如果未被包装则返回原字符串
|
static String |
unWrap(CharSequence str,
String prefix,
String suffix)
去掉字符包装,如果未被包装则返回原字符串
此方法要求prefix和suffix都存在,如果只有一个,不做去除。 |
static String |
upperAt(CharSequence str,
int index)
大写对应下标字母
例如: str = name,index = 1, return nAme
|
static String |
upperFirst(CharSequence str)
大写首字母
例如:str = name, return Name |
static String |
upperFirstAndAddPre(CharSequence str,
String preString)
原字符串首字母大写并在其首部添加指定字符串 例如:str=name, preString=get =》 return getName
|
static String |
wrap(CharSequence str,
char prefix,
char suffix)
包装指定字符串
|
static String |
wrap(CharSequence str,
CharSequence prefixAndSuffix)
包装指定字符串
当前缀和后缀一致时使用此方法 |
static String |
wrap(CharSequence str,
CharSequence prefix,
CharSequence suffix)
包装指定字符串
|
static String[] |
wrapAll(CharSequence prefix,
CharSequence suffix,
CharSequence... strs)
包装多个字符串
|
static String[] |
wrapAllIfMissing(CharSequence prefix,
CharSequence suffix,
CharSequence... strs)
包装多个字符串,如果已经包装,则不再包装
|
static String[] |
wrapAllWithPair(CharSequence prefixAndSuffix,
CharSequence... strs)
使用单个字符包装多个字符串
|
static String[] |
wrapAllWithPairIfMissing(CharSequence prefixAndSuffix,
CharSequence... strs)
使用成对的字符包装多个字符串,如果已经包装,则不再包装
|
static String |
wrapIfMissing(CharSequence str,
CharSequence prefix,
CharSequence suffix)
包装指定字符串,如果前缀或后缀已经包含对应的字符串,则不再包装
|
hasBlank, hasEmpty, hasEmpty, isAllBlank, isAllCharMatch, isAllEmpty, isAllEmpty, isAllNotBlank, isAllNotEmpty, isBlank, isBlankOrUndefined, isEmpty, isEmptyOrUndefined, isNotBlank, isNotEmpty, isNullOrUndefined
public static final int INDEX_NOT_FOUND
Finder.INDEX_NOT_FOUND
,
Constant Field Valuespublic static String toString(Object obj)
obj
- 对象String.valueOf(Object)
public static String toStringOrNull(Object obj)
null
会返回null
obj
- 对象null
public static String toStringOrEmpty(Object obj)
obj
- 对象String
emptyIfNull(CharSequence)
public static CharSequence emptyIfNull(CharSequence str)
str
- 被转换的字符串toStringOrEmpty(Object)
public static <T extends CharSequence> T nullIfEmpty(T str)
null
T
- 字符串类型str
- 被转换的字符串public static <T extends CharSequence> T nullIfBlank(T str)
null
T
- 字符串类型str
- 被转换的字符串public static <T extends CharSequence> T defaultIfNull(T str, T defaultValue)
如果给定字符串为null
返回默认值
defaultIfNull(null, null); // = null
defaultIfNull(null, ""); // = ""
defaultIfNull(null, "zz"); // = "zz"
defaultIfNull("abc", *); // = "abc"
T
- 字符串类型str
- 被检查字符串,可能为null
defaultValue
- 被检查字符串为null
返回的默认值,可以为null
null
返回原值,否则返回默认值ObjUtil.defaultIfNull(Object, Object)
public static <T extends CharSequence> T defaultIfNull(T source, Supplier<? extends T> defaultSupplier)
null
返回原值, 否则返回 Supplier.get()
提供的默认值T
- 被检查字符串类型source
- 被检查字符串,可能为null
defaultSupplier
- 为空时的默认值提供者null
返回原值,否则返回 Supplier.get()
提供的默认值ObjUtil.defaultIfNull(Object, Supplier)
public static <T extends CharSequence,R> R defaultIfNull(T source, Function<? super T,? extends R> handler, Supplier<? extends R> defaultSupplier)
null
返回自定义handler处理后的结果,否则返回 Supplier.get()
提供的默认值R
- 返回值类型T
- 被检查对象类型source
- 被检查对象,可能为null
handler
- 非空时自定义的处理方法defaultSupplier
- 为空时的默认值提供者null
返回处理后的结果,否则返回 Supplier.get()
提供的默认值ObjUtil.defaultIfNull(Object, Function, Supplier)
public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultValue)
null
或者 "" 返回默认值
defaultIfEmpty(null, null) = null defaultIfEmpty(null, "") = "" defaultIfEmpty("", "zz") = "zz" defaultIfEmpty(" ", "zz") = " " defaultIfEmpty("abc", *) = "abc"
T
- 对象类型(必须实现CharSequence接口)str
- 被检查对象,可能为null
defaultValue
- 被检查对象为null
或者 ""返回的默认值,可以为null
或者 ""null
或者 ""返回默认值,否则返回原值public static <T extends CharSequence> T defaultIfEmpty(T str, Supplier<? extends T> defaultSupplier)
null
或者""
返回原值, 否则返回自定义handler处理后的返回值T
- 被检查对象类型str
- String 类型defaultSupplier
- empty时的处理方法public static <T extends CharSequence,V> V defaultIfEmpty(T str, Function<T,V> handler, Supplier<? extends V> defaultSupplier)
null
或者""
返回defaultHandler处理的结果, 否则返回自定义handler处理后的返回值T
- 被检查对象类型V
- 结果类型str
- String 类型handler
- 非empty的处理方法defaultSupplier
- empty时的处理方法public static <T extends CharSequence> T defaultIfBlank(T str, T defaultValue)
null
或者""或者空白符返回默认值
defaultIfBlank(null, null) = null defaultIfBlank(null, "") = "" defaultIfBlank("", "zz") = "zz" defaultIfBlank(" ", "zz") = "zz" defaultIfBlank("abc", *) = "abc"
T
- 对象类型(必须实现CharSequence接口)str
- 被检查对象,可能为null
defaultValue
- 被检查对象为null
或者 ""或者空白符返回的默认值,可以为null
或者 ""或者空白符null
或者 ""或者空白符返回默认值,否则返回原值public static <T extends CharSequence> T defaultIfBlank(T str, Supplier<? extends T> defaultSupplier)
null
或者""
返回原值, 否则返回自定义handler处理后的返回值T
- 被检查对象类型str
- String 类型defaultSupplier
- empty时的处理方法public static <T extends CharSequence,V> V defaultIfBlank(T str, Function<T,V> handler, Supplier<? extends V> defaultSupplier)
null
或 "" 或 空白字符串时,返回默认值(由 defaultValueSupplier 提供);否则直接返回T
- 对象类型(必须实现CharSequence接口)V
- 结果类型str
- 被检查对象handler
- 非blank的处理方法defaultSupplier
- 默认值提供者null
返回默认值,否则返回自定义handle处理后的返回值NullPointerException
- defaultValueSupplier == null
时,抛出public static String trim(CharSequence str)
null
,依然返回null
。
注意,和String.trim()
不同,此方法使用CharUtil.isBlankChar(char)
来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
trimPrefix(CharSequence)
去除头部空格trimSuffix(CharSequence)
去除尾部空格cleanBlank(CharSequence)
去除头部、尾部、中间空格trim(null) = null trim("") = "" trim(" ") = "" trim("abc") = "abc" trim(" abc ") = "abc"
str
- 要处理的字符串null
,则返回null
public static String trimToEmpty(CharSequence str)
null
,返回""
。
trimToEmpty(null) = "" trimToEmpty("") = "" trimToEmpty(" ") = "" trimToEmpty("abc") = "abc" trimToEmpty(" abc ") = "abc"
str
- 字符串public static String trimToNull(CharSequence str)
null
或者"",返回null
。
trimToNull(null) = null trimToNull("") = null trimToNull(" ") = null trimToNull("abc") = "abc" trimToEmpty(" abc ") = "abc"
str
- 字符串public static String trimPrefix(CharSequence str)
null
,则返回null
。
注意,和String.trim()
不同,此方法使用CharUtil.isBlankChar(char)
来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
trimPrefix(null) = null trimPrefix("") = "" trimPrefix("abc") = "abc" trimPrefix(" abc") = "abc" trimPrefix("abc ") = "abc " trimPrefix(" abc ") = "abc "
str
- 要处理的字符串null
或结果字符串为""
,则返回 null
public static String trimSuffix(CharSequence str)
null
,则返回null
。
注意,和String.trim()
不同,此方法使用CharUtil.isBlankChar(char)
来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
trimSuffix(null) = null trimSuffix("") = "" trimSuffix("abc") = "abc" trimSuffix(" abc") = " abc" trimSuffix("abc ") = "abc" trimSuffix(" abc ") = " abc"
str
- 要处理的字符串null
或结果字符串为""
,则返回 null
public static String trim(CharSequence str, StrTrimer.TrimMode mode)
null
,依然返回null
。str
- 要处理的字符串mode
- 去除模式,可选去除头部、尾部、两边null
,则返回null
public static String trim(CharSequence str, StrTrimer.TrimMode mode, Predicate<Character> predicate)
null
,依然返回null
。str
- 要处理的字符串mode
- 去除模式,可选去除头部、尾部、两边predicate
- 断言是否过掉字符,返回true
表述过滤掉,false
表示不过滤null
,则返回null
public static boolean startWith(CharSequence str, char c)
str
- 字符串c
- 字符public static boolean startWith(CharSequence str, CharSequence prefix)
str
- 被监测字符串prefix
- 开头字符串public static boolean startWithIgnoreEquals(CharSequence str, CharSequence prefix)
str
- 被监测字符串prefix
- 开头字符串public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix)
str
- 被监测字符串prefix
- 开头字符串public static boolean startWithAny(CharSequence str, CharSequence... prefixes)
str
- 给定字符串prefixes
- 需要检测的开始字符串public static boolean startWithAnyIgnoreCase(CharSequence str, CharSequence... prefixes)
str
- 给定字符串prefixes
- 需要检测的开始字符串public static boolean startWith(CharSequence str, CharSequence prefix, boolean ignoreCase)
str
- 被监测字符串prefix
- 开头字符串ignoreCase
- 是否忽略大小写public static boolean startWith(CharSequence str, CharSequence prefix, boolean ignoreCase, boolean ignoreEquals)
CharSequenceUtil.startWith("123", "123", false, true); -- false CharSequenceUtil.startWith("ABCDEF", "abc", true, true); -- true CharSequenceUtil.startWith("abc", "abc", true, true); -- false
str
- 被监测字符串prefix
- 开头字符串ignoreCase
- 是否忽略大小写ignoreEquals
- 是否忽略字符串相等的情况public static boolean endWith(CharSequence str, char c)
str
- 字符串c
- 字符public static boolean endWith(CharSequence str, CharSequence suffix)
str
- 被监测字符串suffix
- 结尾字符串public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix)
str
- 被监测字符串suffix
- 结尾字符串public static boolean endWithAny(CharSequence str, CharSequence... suffixes)
str
- 给定字符串suffixes
- 需要检测的结尾字符串public static boolean endWithAnyIgnoreCase(CharSequence str, CharSequence... suffixes)
str
- 给定字符串suffixes
- 需要检测的结尾字符串public static boolean endWith(CharSequence str, CharSequence suffix, boolean ignoreCase)
str
- 被监测字符串suffix
- 结尾字符串ignoreCase
- 是否忽略大小写public static boolean endWith(CharSequence str, CharSequence suffix, boolean ignoreCase, boolean ignoreEquals)
str
- 被监测字符串suffix
- 结尾字符串ignoreCase
- 是否忽略大小写ignoreEquals
- 是否忽略字符串相等的情况public static boolean contains(CharSequence str, char searchChar)
str
- 字符串searchChar
- 被查找的字符public static boolean contains(CharSequence str, CharSequence searchStr)
str
- 字符串searchStr
- 被查找的字符串public static boolean containsAny(CharSequence str, CharSequence... testStrs)
str
- 指定字符串testStrs
- 需要检查的字符串数组public static boolean containsAny(CharSequence str, char... testChars)
str
- 指定字符串testChars
- 需要检查的字符数组public static boolean containsOnly(CharSequence str, char... testChars)
CharSequenceUtil.containsOnly("asdas", 'a', 'd', 's','l'); --> true
str
- 字符串testChars
- 检查的字符public static boolean containsBlank(CharSequence str)
str
- 字符串public static String getContainsStr(CharSequence str, CharSequence... testStrs)
str
- 指定字符串testStrs
- 需要检查的字符串数组public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr)
null
,返回truestr
- 被检测字符串testStr
- 被测试是否包含的字符串public static boolean containsAnyIgnoreCase(CharSequence str, CharSequence... testStrs)
str
- 指定字符串testStrs
- 需要检查的字符串数组public static String getContainsStrIgnoreCase(CharSequence str, CharSequence... testStrs)
str
- 指定字符串testStrs
- 需要检查的字符串数组public static boolean containsAll(CharSequence str, CharSequence... testChars)
str
- 字符串testChars
- 检查的字符public static int indexOf(CharSequence str, char searchChar)
str
- 字符串searchChar
- 被查找的字符public static int indexOf(CharSequence str, char searchChar, int start)
str
- 字符串searchChar
- 被查找的字符start
- 起始位置,如果小于0,从0开始查找public static int indexOf(CharSequence text, char searchChar, int start, int end)
text
- 字符串searchChar
- 被查找的字符start
- 起始位置,如果小于0,从0开始查找end
- 终止位置,如果超过str.length()则默认查找到字符串末尾public static int indexOf(CharSequence text, Predicate<Character> matcher, int start, int end)
text
- 字符串matcher
- 被查找的字符匹配器start
- 起始位置,如果小于0,从0开始查找end
- 终止位置,如果超过str.length()则默认查找到字符串末尾public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr)
indexOfIgnoreCase(null, *, *) = -1 indexOfIgnoreCase(*, null, *) = -1 indexOfIgnoreCase("", "", 0) = 0 indexOfIgnoreCase("aabaabaa", "A", 0) = 0 indexOfIgnoreCase("aabaabaa", "B", 0) = 2 indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 indexOfIgnoreCase("aabaabaa", "B", 3) = 5 indexOfIgnoreCase("aabaabaa", "B", 9) = -1 indexOfIgnoreCase("aabaabaa", "B", -1) = 2 indexOfIgnoreCase("aabaabaa", "", 2) = 2 indexOfIgnoreCase("abc", "", 9) = -1
str
- 字符串searchStr
- 需要查找位置的字符串public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int fromIndex)
indexOfIgnoreCase(null, *, *) = -1 indexOfIgnoreCase(*, null, *) = -1 indexOfIgnoreCase("", "", 0) = 0 indexOfIgnoreCase("aabaabaa", "A", 0) = 0 indexOfIgnoreCase("aabaabaa", "B", 0) = 2 indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 indexOfIgnoreCase("aabaabaa", "B", 3) = 5 indexOfIgnoreCase("aabaabaa", "B", 9) = -1 indexOfIgnoreCase("aabaabaa", "B", -1) = 2 indexOfIgnoreCase("aabaabaa", "", 2) = 2 indexOfIgnoreCase("abc", "", 9) = -1
str
- 字符串searchStr
- 需要查找位置的字符串fromIndex
- 起始位置public static int indexOf(CharSequence text, CharSequence searchStr, int from, boolean ignoreCase)
text
- 字符串,空则返回-1searchStr
- 需要查找位置的字符串,空则返回-1from
- 起始位置(包含)ignoreCase
- 是否忽略大小写public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr)
str
- 字符串searchStr
- 需要查找位置的字符串public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr, int fromIndex)
str
- 字符串searchStr
- 需要查找位置的字符串fromIndex
- 起始位置,从后往前计数public static int lastIndexOf(CharSequence text, CharSequence searchStr, int from, boolean ignoreCase)
text
- 字符串searchStr
- 需要查找位置的字符串from
- 起始位置,从后往前计数ignoreCase
- 是否忽略大小写public static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal)
如果 str=null 或 searchStr=null 或 ordinal≤0 则返回-1
此方法来自:Apache-Commons-Lang
例子(*代表任意字符):
ordinalIndexOf(null, *, *) = -1 ordinalIndexOf(*, null, *) = -1 ordinalIndexOf("", "", *) = 0 ordinalIndexOf("aabaabaa", "a", 1) = 0 ordinalIndexOf("aabaabaa", "a", 2) = 1 ordinalIndexOf("aabaabaa", "b", 1) = 2 ordinalIndexOf("aabaabaa", "b", 2) = 5 ordinalIndexOf("aabaabaa", "ab", 1) = 1 ordinalIndexOf("aabaabaa", "ab", 2) = 4 ordinalIndexOf("aabaabaa", "", 1) = 0 ordinalIndexOf("aabaabaa", "", 2) = 0
str
- 被检查的字符串,可以为nullsearchStr
- 被查找的字符串,可以为nullordinal
- 第几次出现的位置public static String removeAll(CharSequence str, CharSequence strToRemove)
str
- 字符串strToRemove
- 被移除的字符串public static String removeAll(CharSequence str, CharSequence... strsToRemove)
str
- 字符串strsToRemove
- 被移除的字符串public static String removeAll(CharSequence str, char... chars)
str
- 字符串chars
- 字符列表public static String removeAllLineBreaks(CharSequence str)
1. \r 1. \n
str
- 字符串public static String removePreAndLowerFirst(CharSequence str, int preLength)
str
- 被处理的字符串preLength
- 去掉的长度public static String removePreAndLowerFirst(CharSequence str, CharSequence prefix)
str
- 被处理的字符串prefix
- 前缀public static String removeAllPrefix(CharSequence str, CharSequence prefix)
str=abcdef, prefix=ab => return cdef
str=ababcdef, prefix=ab => return cdef
str=ababcdef, prefix="" => return ababcdef
str=ababcdef, prefix=null => return ababcdef
str
- 字符串,空返回原字符串prefix
- 前缀,空返回原字符串public static String removePrefix(CharSequence str, CharSequence prefix)
str
- 字符串,空返回原字符串prefix
- 前缀,空返回原字符串public static String removePrefixIgnoreCase(CharSequence str, CharSequence prefix)
str
- 字符串,空返回原字符串prefix
- 前缀,空返回原字符串public static String removePrefix(CharSequence str, CharSequence prefix, boolean ignoreCase)
str
- 字符串,空返回原字符串prefix
- 前缀,空返回原字符串ignoreCase
- 是否忽略大小写public static String removeSufAndLowerFirst(CharSequence str, CharSequence suffix)
str
- 字符串suffix
- 后缀public static String removeAllSuffix(CharSequence str, CharSequence suffix)
str=11abab, suffix=ab => return 11
str=11ab, suffix=ab => return 11
str=11ab, suffix="" => return 11ab
str=11ab, suffix=null => return 11ab
str
- 字符串,空返回原字符串suffix
- 后缀字符串,空返回原字符串public static String removeSuffix(CharSequence str, CharSequence suffix)
str
- 字符串suffix
- 后缀public static String removeSuffixIgnoreCase(CharSequence str, CharSequence suffix)
str
- 字符串suffix
- 后缀public static String cleanBlank(CharSequence str)
str
- 被清理的字符串public static String stripIgnoreCase(CharSequence str, CharSequence prefixOrSuffix)
str
- 被处理的字符串prefixOrSuffix
- 前缀或后缀public static String stripIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix)
str
- 被处理的字符串prefix
- 前缀suffix
- 后缀public static String strip(CharSequence str, CharSequence prefixOrSuffix)
"aaa_STRIPPED_bbb", "a" -> "aa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "" -> "aaa_STRIPPED_bbb"
str
- 被处理的字符串,null
忽略prefixOrSuffix
- 前缀或后缀public static String strip(CharSequence str, CharSequence prefix, CharSequence suffix)
"aaa_STRIPPED_bbb", "a", "b" -> "aa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", "a", "" -> "aa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "aa_STRIPPED_bbb"
"a", "a", "a" -> ""
str
- 被处理的字符串prefix
- 前缀,null
忽略suffix
- 后缀,null
忽略public static String strip(CharSequence str, CharSequence prefix, CharSequence suffix, boolean ignoreCase)
"aaa_STRIPPED_bbb", "a", "b" -> "aa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_bb"
"aaa_STRIPPED_bbb", "a", "" -> "aa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "aa_STRIPPED_bbb"
"a", "a", "a" -> ""
str
- 被处理的字符串prefix
- 前缀,null
忽略suffix
- 后缀,null
忽略ignoreCase
- 是否忽略大小写public static String stripAll(CharSequence str, CharSequence prefixOrSuffix)
"aaa_STRIPPED_bbb", "a" -> "_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", "b" -> "_STRIPPED_"
"aaa_STRIPPED_bbb", "" -> "aaa_STRIPPED_bbb"
str
- 被处理的字符串prefixOrSuffix
- 前缀或后缀,null
忽略public static String stripAll(CharSequence str, CharSequence prefix, CharSequence suffix)
"aaa_STRIPPED_bbb", "a", "b" -> "_STRIPPED_"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", "a", "" -> "_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "_STRIPPED_bbb"
// special test
"aaaaaabbb", "aaa", null -> "bbb"
"aaaaaaabbb", "aa", null -> "abbb"
"aaaaaaaaa", "aaa", "aa" -> ""
"a", "a", "a" -> ""
str
- 被处理的字符串prefix
- 前缀suffix
- 后缀public static String stripAll(CharSequence str, CharSequence prefix, CharSequence suffix, boolean ignoreCase)
"aaa_STRIPPED_bbb", "a", "b" -> "_STRIPPED_"
"aaa_STRIPPED_bbb", null, null -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "" -> "aaa_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "", "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", null, "b" -> "aaa_STRIPPED_"
"aaa_STRIPPED_bbb", "a", "" -> "_STRIPPED_bbb"
"aaa_STRIPPED_bbb", "a", null -> "_STRIPPED_bbb"
// special test
"aaaaaabbb", "aaa", null -> "bbb"
"aaaaaaabbb", "aa", null -> "abbb"
"aaaaaaaaa", "aaa", "aa" -> ""
"a", "a", "a" -> ""
str
- 被处理的字符串prefix
- 前缀suffix
- 后缀ignoreCase
- 是否忽略大小写public static String addPrefixIfNot(CharSequence str, CharSequence prefix)
str
- 字符串prefix
- 前缀prependIfMissing(CharSequence, CharSequence, CharSequence...)
public static String addSuffixIfNot(CharSequence str, CharSequence suffix)
str
- 字符串suffix
- 后缀appendIfMissing(CharSequence, CharSequence, CharSequence...)
public static String[] cut(CharSequence str, int partLength)
str
- 字符串partLength
- 每等份的长度public static String sub(CharSequence str, int fromIndexInclude, int toIndexExclude)
str
- StringfromIndexInclude
- 开始的index(包括)toIndexExclude
- 结束的index(不包括)public static String subByCodePoint(CharSequence str, int fromIndex, int toIndex)
str
- StringfromIndex
- 开始的index(包括)toIndex
- 结束的index(不包括)public static String subPreGbk(CharSequence str, int len, CharSequence suffix)
str
- 字符串len
- bytes切割到的位置(包含)suffix
- 切割后加上后缀public static String subPreGbk(CharSequence str, int len, boolean halfUp)
true
表示保留,则长度是11,否则长度9str
- 字符串len
- bytes切割到的位置(包含)halfUp
- 遇到截取一半的GBK字符,是否保留。public static String subPre(CharSequence string, int toIndexExclude)
安全的subString,允许:string为null,允许string长度小于toIndexExclude长度
assertEquals(subPre(null, 3), null);
assertEquals(subPre("ab", 3), "ab");
assertEquals(subPre("abc", 3), "abc");
assertEquals(subPre("abcd", 3), "abc");
assertEquals(subPre("abcd", -3), "a");
assertEquals(subPre("ab", 3), "ab");
string
- 字符串toIndexExclude
- 切割到的位置(不包括)public static String subSuf(CharSequence string, int fromIndex)
string
- 字符串fromIndex
- 切割开始的位置(包括)public static String subSufByLength(CharSequence string, int length)
subSufByLength("abcde", 3) = "cde" subSufByLength("abcde", 0) = "" subSufByLength("abcde", -5) = "" subSufByLength("abcde", -1) = "" subSufByLength("abcde", 5) = "abcde" subSufByLength("abcde", 10) = "abcde" subSufByLength(null, 3) = null
string
- 字符串length
- 切割长度public static String subByLength(String input, int fromIndex, int length)
0 1 2 3 4 A B C D当fromIndex为负数时,为反向插空位置,其中-1表示最后一个字符之前的位置:
-3 -2 -1 length A B C D
input
- 原始字符串fromIndex
- 开始的index,包括,可以为负数length
- 要截取的长度public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator)
subBefore(null, *, false) = null subBefore("", *, false) = "" subBefore("abc", "a", false) = "" subBefore("abcba", "b", false) = "a" subBefore("abc", "c", false) = "ab" subBefore("abc", "d", false) = "abc" subBefore("abc", "", false) = "" subBefore("abc", null, false) = "abc"
string
- 被查找的字符串separator
- 分隔字符串(不包括)isLastSeparator
- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个public static String subBefore(CharSequence string, char separator, boolean isLastSeparator)
subBefore(null, *, false) = null subBefore("", *, false) = "" subBefore("abc", 'a', false) = "" subBefore("abcba", 'b', false) = "a" subBefore("abc", 'c', false) = "ab" subBefore("abc", 'd', false) = "abc"
string
- 被查找的字符串separator
- 分隔字符串(不包括)isLastSeparator
- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator)
subAfter(null, *, false) = null subAfter("", *, false) = "" subAfter(*, null, false) = "" subAfter("abc", "a", false) = "bc" subAfter("abcba", "b", false) = "cba" subAfter("abc", "c", false) = "" subAfter("abc", "d", false) = "" subAfter("abc", "", false) = "abc"
string
- 被查找的字符串separator
- 分隔字符串(不包括)isLastSeparator
- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个public static String subAfter(CharSequence string, char separator, boolean isLastSeparator)
subAfter(null, *, false) = null subAfter("", *, false) = "" subAfter("abc", 'a', false) = "bc" subAfter("abcba", 'b', false) = "cba" subAfter("abc", 'c', false) = "" subAfter("abc", 'd', false) = ""
string
- 被查找的字符串separator
- 分隔字符串(不包括)isLastSeparator
- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个public static String subBetween(CharSequence str, CharSequence before, CharSequence after)
栗子:
subBetween("wx[b]yz", "[", "]") = "b" subBetween(null, *, *) = null subBetween(*, null, *) = null subBetween(*, *, null) = null subBetween("", "", "") = "" subBetween("", "", "]") = null subBetween("", "[", "]") = null subBetween("yabcz", "", "") = "" subBetween("yabcz", "y", "z") = "abc" subBetween("yabczyabcz", "y", "z") = "abc"
str
- 被切割的字符串before
- 截取开始的字符串标识after
- 截取到的字符串标识public static String subBetween(CharSequence str, CharSequence beforeAndAfter)
栗子:
subBetween(null, *) = null subBetween("", "") = "" subBetween("", "tag") = null subBetween("tagabctag", null) = null subBetween("tagabctag", "") = "" subBetween("tagabctag", "tag") = "abc"
str
- 被切割的字符串beforeAndAfter
- 截取开始和结束的字符串标识public static String[] subBetweenAll(CharSequence str, CharSequence prefix, CharSequence suffix)
栗子:
subBetweenAll("wx[b]y[z]", "[", "]") = ["b","z"] subBetweenAll(null, *, *) = [] subBetweenAll(*, null, *) = [] subBetweenAll(*, *, null) = [] subBetweenAll("", "", "") = [] subBetweenAll("", "", "]") = [] subBetweenAll("", "[", "]") = [] subBetweenAll("yabcz", "", "") = [] subBetweenAll("yabcz", "y", "z") = ["abc"] subBetweenAll("yabczyabcz", "y", "z") = ["abc","abc"] subBetweenAll("[yabc[zy]abcz]", "[", "]"); = ["zy"] 重叠时只截取内部,
str
- 被切割的字符串prefix
- 截取开始的字符串标识suffix
- 截取到的字符串标识public static String[] subBetweenAll(CharSequence str, CharSequence prefixAndSuffix)
栗子:
subBetweenAll(null, *) = [] subBetweenAll(*, null) = [] subBetweenAll(*, *) = [] subBetweenAll("", "") = [] subBetweenAll("", "#") = [] subBetweenAll("gotanks", "") = [] subBetweenAll("#gotanks#", "#") = ["gotanks"] subBetweenAll("#hello# #world#!", "#") = ["hello", "world"] subBetweenAll("#hello# world#!", "#"); = ["hello"]
str
- 被切割的字符串prefixAndSuffix
- 截取开始和结束的字符串标识public static String repeat(char c, int count)
repeat('e', 0) = "" repeat('e', 3) = "eee" repeat('e', -2) = ""
c
- 被重复的字符count
- 重复的数目,如果小于等于0则返回""public static String repeat(CharSequence str, int count)
str
- 被重复的字符count
- 重复的数目public static String repeatByLength(CharSequence str, int padLen)
str
- 被重复的字符padLen
- 指定长度public static String repeatAndJoin(CharSequence str, int count, CharSequence delimiter)
repeatAndJoin("?", 5, ",") = "?,?,?,?,?" repeatAndJoin("?", 0, ",") = "" repeatAndJoin("?", 5, null) = "?????"
str
- 被重复的字符串count
- 数量delimiter
- 分界符public static boolean equals(CharSequence str1, CharSequence str2)
equals(null, null) = true equals(null, "abc") = false equals("abc", null) = false equals("abc", "abc") = true equals("abc", "ABC") = false
str1
- 要比较的字符串1str2
- 要比较的字符串2null
,则返回true
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2)
equalsIgnoreCase(null, null) = true equalsIgnoreCase(null, "abc") = false equalsIgnoreCase("abc", null) = false equalsIgnoreCase("abc", "abc") = true equalsIgnoreCase("abc", "ABC") = true
str1
- 要比较的字符串1str2
- 要比较的字符串2null
,则返回true
public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase)
null
String.equalsIgnoreCase(String)
判断相等String.contentEquals(CharSequence)
判断相等str1
- 要比较的字符串1str2
- 要比较的字符串2ignoreCase
- 是否忽略大小写null
,则返回true
public static boolean equalsAnyIgnoreCase(CharSequence str1, CharSequence... strs)
true
,没有相同的返回false
false
str1
- 给定需要检查的字符串strs
- 需要参与比对的字符串列表public static boolean equalsAny(CharSequence str1, CharSequence... strs)
true
,没有相同的返回false
false
str1
- 给定需要检查的字符串strs
- 需要参与比对的字符串列表public static boolean equalsAny(CharSequence str1, boolean ignoreCase, CharSequence... strs)
true
,没有相同的返回false
false
str1
- 给定需要检查的字符串ignoreCase
- 是否忽略大小写strs
- 需要参与比对的字符串列表public static boolean equalsCharAt(CharSequence str, int position, char c)
str
- 字符串position
- 位置c
- 需要对比的字符public static boolean isSubEquals(CharSequence str1, int offset1, CharSequence str2, boolean ignoreCase)
str1
- 第一个字符串offset1
- 第一个字符串开始的位置str2
- 第二个字符串ignoreCase
- 是否忽略大小写String.regionMatches(boolean, int, String, int, int)
public static boolean isSubEquals(CharSequence str1, int offset1, CharSequence str2, int offset2, int length, boolean ignoreCase)
str1
- 第一个字符串offset1
- 第一个字符串开始的位置str2
- 第二个字符串offset2
- 第二个字符串开始的位置length
- 截取长度ignoreCase
- 是否忽略大小写String.regionMatches(boolean, int, String, int, int)
public static String format(CharSequence template, Object... params)
template
- 文本模板,被替换的部分用 {} 表示,如果模板为null,返回"null"params
- 参数值public static String indexedFormat(CharSequence pattern, Object... arguments)
pattern
- 文本格式arguments
- 参数public static String formatByMap(CharSequence template, Map<?,?> map)
template
- 文本模板,被替换的部分用 {key} 表示map
- 参数值对public static String formatByMap(CharSequence template, Map<?,?> map, boolean ignoreNull)
template
- 文本模板,被替换的部分用 {key} 表示map
- 参数值对ignoreNull
- 是否忽略 null
值,忽略则 null
值对应的变量不被替换,否则替换为""public static String formatByBean(CharSequence template, Object bean, boolean ignoreNull)
template
- 文本模板,被替换的部分用 {key} 表示bean
- 参数BeanignoreNull
- 是否忽略 null
值,忽略则 null
值对应的变量不被替换,否则替换为""public static String wrap(CharSequence str, CharSequence prefixAndSuffix)
str
- 被包装的字符串prefixAndSuffix
- 前缀和后缀public static String wrap(CharSequence str, CharSequence prefix, CharSequence suffix)
str
- 被包装的字符串prefix
- 前缀suffix
- 后缀public static String wrap(CharSequence str, char prefix, char suffix)
str
- 被包装的字符串prefix
- 前缀suffix
- 后缀public static String[] wrapAllWithPair(CharSequence prefixAndSuffix, CharSequence... strs)
prefixAndSuffix
- 前缀和后缀strs
- 多个字符串public static String[] wrapAll(CharSequence prefix, CharSequence suffix, CharSequence... strs)
prefix
- 前缀suffix
- 后缀strs
- 多个字符串public static String wrapIfMissing(CharSequence str, CharSequence prefix, CharSequence suffix)
str
- 被包装的字符串prefix
- 前缀suffix
- 后缀public static String[] wrapAllWithPairIfMissing(CharSequence prefixAndSuffix, CharSequence... strs)
prefixAndSuffix
- 前缀和后缀strs
- 多个字符串public static String[] wrapAllIfMissing(CharSequence prefix, CharSequence suffix, CharSequence... strs)
prefix
- 前缀suffix
- 后缀strs
- 多个字符串public static String unWrap(CharSequence str, String prefix, String suffix)
str
- 字符串prefix
- 前置字符串suffix
- 后置字符串public static String unWrap(CharSequence str, char prefix, char suffix)
str
- 字符串prefix
- 前置字符suffix
- 后置字符public static String unWrap(CharSequence str, char prefixAndSuffix)
str
- 字符串prefixAndSuffix
- 前置和后置字符public static boolean isWrap(CharSequence str, CharSequence prefix, CharSequence suffix)
str
- 字符串prefix
- 前缀suffix
- 后缀public static boolean isWrap(CharSequence str, String wrapper)
str
- 字符串wrapper
- 包装字符串public static boolean isWrap(CharSequence str, char wrapper)
str
- 字符串wrapper
- 包装字符public static boolean isWrap(CharSequence str, char prefixChar, char suffixChar)
str
- 字符串prefixChar
- 前缀suffixChar
- 后缀public static String padPre(CharSequence str, int length, CharSequence padStr)
padPre(null, *, *);//null padPre("1", 3, "ABC");//"AB1" padPre("123", 2, "ABC");//"12" padPre("1039", -1, "0");//"103"
str
- 字符串length
- 长度padStr
- 补充的字符public static String padPre(CharSequence str, int length, char padChar)
padPre(null, *, *);//null padPre("1", 3, '0');//"001" padPre("123", 2, '0');//"12"
str
- 字符串length
- 长度padChar
- 补充的字符public static String padAfter(CharSequence str, int length, char padChar)
padAfter(null, *, *);//null padAfter("1", 3, '0');//"100" padAfter("123", 2, '0');//"23" padAfter("123", -1, '0')//"" 空串
str
- 字符串,如果为null
,直接返回nulllength
- 长度padChar
- 补充的字符public static String padAfter(CharSequence str, int length, CharSequence padStr)
padAfter(null, *, *);//null padAfter("1", 3, "ABC");//"1AB" padAfter("123", 2, "ABC");//"23"
str
- 字符串,如果为null
,直接返回nulllength
- 长度padStr
- 补充的字符public static String center(CharSequence str, int size)
center(null, *) = null center("", 4) = " " center("ab", -1) = "ab" center("ab", 4) = " ab " center("abcd", 2) = "abcd" center("a", 4) = " a "
str
- 字符串size
- 指定长度public static String center(CharSequence str, int size, char padChar)
center(null, *, *) = null center("", 4, ' ') = " " center("ab", -1, ' ') = "ab" center("ab", 4, ' ') = " ab " center("abcd", 2, ' ') = "abcd" center("a", 4, ' ') = " a " center("a", 4, 'y') = "yayy" center("abc", 7, ' ') = " abc "
str
- 字符串size
- 指定长度padChar
- 两边补充的字符public static String center(CharSequence str, int size, CharSequence padStr)
center(null, *, *) = null center("", 4, " ") = " " center("ab", -1, " ") = "ab" center("ab", 4, " ") = " ab " center("abcd", 2, " ") = "abcd" center("a", 4, " ") = " a " center("a", 4, "yz") = "yayz" center("abc", 7, null) = " abc " center("abc", 7, "") = " abc "
str
- 字符串size
- 指定长度padStr
- 两边补充的字符串public static int count(CharSequence content, CharSequence strForSearch)
null
或者 "" 返回 0
.
count(null, *) = 0 count("", *) = 0 count("abba", null) = 0 count("abba", "") = 0 count("abba", "a") = 2 count("abba", "ab") = 1 count("abba", "xxx") = 0
content
- 被查找的字符串strForSearch
- 需要查找的字符串public static int count(CharSequence content, char charForSearch)
content
- 内容charForSearch
- 被统计的字符public static int compare(CharSequence str1, CharSequence str2, boolean nullIsLess)
compare(null, null, *) = 0 compare(null , "a", true) < 0 compare(null , "a", false) > 0 compare("a", null, true) > 0 compare("a", null, false) < 0 compare("abc", "abc", *) = 0 compare("a", "b", *) < 0 compare("b", "a", *) > 0 compare("a", "B", *) > 0 compare("ab", "abc", *) < 0
str1
- 字符串1str2
- 字符串2nullIsLess
- null
值是否排在前(null是否小于非空值)public static int compareIgnoreCase(CharSequence str1, CharSequence str2, boolean nullIsLess)
compareIgnoreCase(null, null, *) = 0 compareIgnoreCase(null , "a", true) < 0 compareIgnoreCase(null , "a", false) > 0 compareIgnoreCase("a", null, true) > 0 compareIgnoreCase("a", null, false) < 0 compareIgnoreCase("abc", "abc", *) = 0 compareIgnoreCase("abc", "ABC", *) = 0 compareIgnoreCase("a", "b", *) < 0 compareIgnoreCase("b", "a", *) > 0 compareIgnoreCase("a", "B", *) < 0 compareIgnoreCase("A", "b", *) < 0 compareIgnoreCase("ab", "abc", *) < 0
str1
- 字符串1str2
- 字符串2nullIsLess
- null
值是否排在前(null是否小于非空值)public static int compareVersion(CharSequence version1, CharSequence version2)
compareVersion(null, "v1") < 0 compareVersion("v1", "v1") = 0 compareVersion(null, null) = 0 compareVersion("v1", null) > 0 compareVersion("1.0.0", "1.0.2") < 0 compareVersion("1.0.2", "1.0.2a") < 0 compareVersion("1.13.0", "1.12.1c") > 0 compareVersion("V0.0.20170102", "V0.0.20170101") > 0
version1
- 版本1version2
- 版本2public static String appendIfMissing(CharSequence str, CharSequence suffix, CharSequence... suffixes)
str
- 被检查的字符串suffix
- 需要添加到结尾的字符串suffixes
- 需要额外检查的结尾字符串,如果以这些中的一个为结尾,则不再添加public static String appendIfMissingIgnoreCase(CharSequence str, CharSequence suffix, CharSequence... suffixes)
str
- 被检查的字符串suffix
- 需要添加到结尾的字符串suffixes
- 需要额外检查的结尾字符串,如果以这些中的一个为结尾,则不再添加public static String appendIfMissing(CharSequence str, CharSequence suffix, boolean ignoreCase, CharSequence... testSuffixes)
str
- 被检查的字符串suffix
- 需要添加到结尾的字符串,不参与检查匹配ignoreCase
- 检查结尾时是否忽略大小写testSuffixes
- 需要额外检查的结尾字符串,如果以这些中的一个为结尾,则不再添加public static String prependIfMissing(CharSequence str, CharSequence prefix, CharSequence... prefixes)
str
- 被检查的字符串prefix
- 需要添加到首部的字符串prefixes
- 需要额外检查的首部字符串,如果以这些中的一个为起始,则不再添加public static String prependIfMissingIgnoreCase(CharSequence str, CharSequence prefix, CharSequence... prefixes)
str
- 被检查的字符串prefix
- 需要添加到首部的字符串prefixes
- 需要额外检查的首部字符串,如果以这些中的一个为起始,则不再添加public static String prependIfMissing(CharSequence str, CharSequence prefix, boolean ignoreCase, CharSequence... prefixes)
str
- 被检查的字符串prefix
- 需要添加到首部的字符串ignoreCase
- 检查结尾时是否忽略大小写prefixes
- 需要额外检查的首部字符串,如果以这些中的一个为起始,则不再添加public static String replaceFirst(CharSequence str, CharSequence searchStr, CharSequence replacedStr, boolean ignoreCase)
str
- 字符串searchStr
- 被查找的字符串replacedStr
- 被替换的字符串ignoreCase
- 是否忽略大小写public static String replaceLast(CharSequence str, CharSequence searchStr, CharSequence replacedStr, boolean ignoreCase)
str
- 字符串searchStr
- 被查找的字符串replacedStr
- 被替换的字符串ignoreCase
- 是否忽略大小写public static String replaceIgnoreCase(CharSequence str, CharSequence searchStr, CharSequence replacement)
str
- 字符串searchStr
- 被查找的字符串replacement
- 被替换的字符串public static String replace(CharSequence str, CharSequence searchStr, CharSequence replacement)
str
- 字符串searchStr
- 被查找的字符串replacement
- 被替换的字符串public static String replace(CharSequence str, CharSequence searchStr, CharSequence replacement, boolean ignoreCase)
str
- 字符串searchStr
- 被查找的字符串replacement
- 被替换的字符串ignoreCase
- 是否忽略大小写public static String replace(CharSequence str, int fromIndex, CharSequence searchStr, CharSequence replacement, boolean ignoreCase)
str
- 字符串fromIndex
- 开始位置(包括)searchStr
- 被查找的字符串replacement
- 被替换的字符串ignoreCase
- 是否忽略大小写public static String replaceByCodePoint(CharSequence str, int beginInclude, int endExclude, char replacedChar)
str
- 字符串beginInclude
- 开始位置(包含)endExclude
- 结束位置(不包含)replacedChar
- 被替换的字符public static String replaceByCodePoint(CharSequence str, int beginInclude, int endExclude, CharSequence replacedStr)
CharSequence.codePoints()
完成拆分替换str
- 字符串beginInclude
- 开始位置(包含)endExclude
- 结束位置(不包含)replacedStr
- 被替换的字符串public static String replace(CharSequence str, Pattern pattern, SerFunction<Matcher,String> replaceFun)
replace(this.content, "(\\d+)", parameters -> "-" + parameters.group(1) + "-") // 结果为:"ZZZaaabbbccc中文-1234-"
str
- 要替换的字符串pattern
- 用于匹配的正则式replaceFun
- 决定如何替换的函数ReUtil.replaceAll(CharSequence, Pattern, SerFunction)
public static String replace(CharSequence str, String regex, SerFunction<Matcher,String> replaceFun)
str
- 要替换的字符串regex
- 用于匹配的正则式replaceFun
- 决定如何替换的函数ReUtil.replaceAll(CharSequence, String, SerFunction)
public static String hide(CharSequence str, int startInclude, int endExclude)
hide(null,*,*)=null hide("",0,*)="" hide("jackduan@163.com",-1,4) ****duan@163.com hide("jackduan@163.com",2,3) ja*kduan@163.com hide("jackduan@163.com",3,2) jackduan@163.com hide("jackduan@163.com",16,16) jackduan@163.com hide("jackduan@163.com",16,17) jackduan@163.com
str
- 字符串startInclude
- 开始位置(包含)endExclude
- 结束位置(不包含)public static String replaceChars(CharSequence str, String chars, CharSequence replacedStr)
str
- 被检查的字符串chars
- 需要替换的字符列表,用一个字符串表示这个字符列表replacedStr
- 替换成的字符串public static String replaceChars(CharSequence str, char[] chars, CharSequence replacedStr)
str
- 被检查的字符串chars
- 需要替换的字符列表replacedStr
- 替换成的字符串public static String replaceAt(CharSequence str, int index, UnaryOperator<Character> operator)
str
- 字符串index
- 位置,-1表示最后一个字符operator
- 替换逻辑,给定原字符,返回新字符public static int length(CharSequence cs)
cs
- a 字符串public static int codeLength(CharSequence cs)
null
返回0cs
- a 字符串null
返回0public static int byteLength(CharSequence cs, Charset charset)
cs
- 字符串charset
- 编码public static int totalLength(CharSequence... strs)
strs
- 字符串数组public static String limitLength(CharSequence string, int length)
string
- 字符串length
- 最大长度public static String limitByteLengthUtf8(CharSequence str, int maxBytesLength, boolean appendDots)
str
- 原始字符串maxBytesLength
- 最大字节数appendDots
- 截断后是否追加省略号(...)public static String limitByteLength(CharSequence str, Charset charset, int maxBytesLength, int factor, boolean appendDots)
str
- 原始字符串charset
- 指定编码maxBytesLength
- 最大字节数factor
- 速算因子,取该编码下单个字符的最大可能字节数appendDots
- 截断后是否追加省略号(...)public static <T extends CharSequence> T firstNonNull(T... strs)
null
元素T
- 元素类型strs
- 多个元素null
public static <T extends CharSequence> T firstNonEmpty(T... strs)
T
- 元素类型strs
- 多个元素null
StrValidator.isNotEmpty(CharSequence)
public static <T extends CharSequence> T firstNonBlank(T... strs)
T
- 元素类型strs
- 多个元素null
StrValidator.isNotBlank(CharSequence)
public static String upperFirstAndAddPre(CharSequence str, String preString)
str
- 被处理的字符串preString
- 添加的首部public static String upperFirst(CharSequence str)
str
- 字符串public static String upperAt(CharSequence str, int index)
例如: str = name,index = 1, return nAme
str
- 字符串index
- 下标,支持负数,-1表示最后一个字符public static String lowerFirst(CharSequence str)
str
- 字符串public static String lowerAt(CharSequence str, int index)
str
- 字符串index
- 下标,支持负数,-1表示最后一个字符public static String filter(CharSequence str, Predicate<Character> predicate)
str
- 字符串predicate
- 过滤器,Predicate.test(Object)
为true
保留字符public static boolean isUpperCase(CharSequence str)
1. 大写字母包括A-Z 2. 其它非字母的Unicode符都算作大写
str
- 被检查的字符串public static boolean isLowerCase(CharSequence str)
1. 小写字母包括a-z 2. 其它非字母的Unicode符都算作小写
str
- 被检查的字符串public static String swapCase(String str)
swapCase(null) = null swapCase("") = "" swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
str
- 字符串public static String toUnderlineCase(CharSequence str)
HelloWorld=》hello_world Hello_World=》hello_world HelloWorld_test=》hello_world_test
str
- 转换前的驼峰式命名的字符串,也可以为下划线形式NamingCase.toUnderlineCase(CharSequence)
public static String toSymbolCase(CharSequence str, char symbol)
str
- 转换前的驼峰式命名的字符串,也可以为符号连接形式symbol
- 连接符NamingCase.toSymbolCase(CharSequence, char)
public static String toCamelCase(CharSequence name)
name
- 转换前的下划线大写方式命名的字符串NamingCase.toCamelCase(CharSequence)
public static String toCamelCase(CharSequence name, char symbol)
name
- 转换前的下划线大写方式命名的字符串symbol
- 连接符NamingCase.toCamelCase(CharSequence, char)
public static StringBuilder builder(CharSequence str)
StringBuilder
,直接返回,否则新建str
- CharSequence
public static StringBuilder builder(CharSequence... strs)
strs
- 初始字符串列表public static String concat(boolean isNullToEmpty, CharSequence... strs)
isNullToEmpty
- 是否null转为""strs
- 字符串数组public static String brief(CharSequence str, int maxLength)
str
- 字符串maxLength
- 结果的最大长度public static String join(CharSequence conjunction, Object... objs)
conjunction
- 分隔符 StrPool.COMMA
objs
- 数组ArrayUtil.join(Object, CharSequence)
public static <T> String join(CharSequence conjunction, Iterable<T> iterable)
T
- 元素类型conjunction
- 分隔符 StrPool.COMMA
iterable
- 集合CollUtil.join(Iterable, CharSequence)
public static boolean isNumeric(CharSequence str)
str
- 字符串public static String move(CharSequence str, int startInclude, int endExclude, int moveLength)
str
- 字符串startInclude
- 起始位置(包括)endExclude
- 结束位置(不包括)moveLength
- 移动距离,负数表示左移,正数为右移public static boolean isCharEquals(CharSequence str)
str
- 字符出啊public static String normalize(CharSequence str)
str
- 归一化的字符串Normalizer.normalize(CharSequence, Normalizer.Form)
public static String fixLength(CharSequence str, char fixedChar, int length)
str
- 字符串fixedChar
- 补充的字符length
- 补充到的长度public static CharSequence commonPrefix(CharSequence str1, CharSequence str2)
commonPrefix("abb", "acc") // "a"
str1
- 字符串1str2
- 字符串2public static CharSequence commonSuffix(CharSequence str1, CharSequence str2)
commonSuffix("aba", "cba") // "ba"
str1
- 字符串1str2
- 字符串2public static int[] toChars(CharSequence str, boolean isCodePoint)
str
- 字符串isCodePoint
- 是否为Unicode码点(即支持emoji等多char字符)public static void forEach(CharSequence str, Consumer<Character> consumer)
str
- 字符串consumer
- 字符处理public static void forEach(CharSequence str, boolean isCodePoint, IntConsumer consumer)
str
- 字符串isCodePoint
- 是否为Unicode码点(即支持emoji等多char字符)consumer
- 字符处理Copyright © 2025. All rights reserved.