public class StrUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
EMPTY |
static int |
INDEX_NOT_FOUND |
Constructor and Description |
---|
StrUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
arrayToString(Object obj)
数组或集合转String
|
static boolean |
contains(CharSequence str,
char searchChar)
指定字符是否在字符串中出现过
|
static boolean |
containsAny(CharSequence str,
CharSequence... testStrs)
查找指定字符串是否包含指定字符串列表中的任意一个字符串
|
static boolean |
containsIgnoreCase(CharSequence str,
CharSequence testStr)
是否包含特定字符,忽略大小写,如果给定两个参数都为
null ,返回true |
static Object |
convert(Class<?> type,
String value) |
static Object |
convert(Class<?> type,
String[] values) |
static boolean |
endWith(CharSequence str,
char c)
字符串是否以给定字符结尾
|
static boolean |
endWith(CharSequence str,
CharSequence suffix)
是否以指定字符串结尾
|
static boolean |
endWith(CharSequence str,
CharSequence suffix,
boolean isIgnoreCase)
是否以指定字符串结尾
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false |
static boolean |
endWithIgnoreCase(CharSequence str,
CharSequence suffix)
是否以指定字符串结尾,忽略大小写
|
static boolean |
equals(CharSequence str1,
CharSequence str2)
比较两个字符串(大小写敏感)。
|
static boolean |
equals(CharSequence str1,
CharSequence str2,
boolean ignoreCase)
比较两个字符串是否相等。
|
static boolean |
equalsIgnoreCase(CharSequence str1,
CharSequence str2)
比较两个字符串(大小写不敏感)。
|
static String |
fill(String str,
char filledChar,
int len,
boolean isPre)
将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
|
static String |
fillAfter(String str,
char filledChar,
int len)
将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
字符填充于字符串后 |
static String |
getContainsStr(CharSequence str,
CharSequence... testStrs)
查找指定字符串是否包含指定字符串列表中的任意一个字符串,如果包含返回找到的第一个字符串
|
static int |
indexOf(CharSequence str,
char searchChar)
指定范围内查找指定字符
|
static int |
indexOf(CharSequence str,
char searchChar,
int start)
指定范围内查找指定字符
|
static int |
indexOf(CharSequence str,
char searchChar,
int start,
int end)
指定范围内查找指定字符
|
static int |
indexOf(CharSequence str,
CharSequence searchStr,
int fromIndex,
boolean ignoreCase)
指定范围内反向查找字符串
|
static int |
indexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
指定范围内查找字符串,忽略大小写
|
static int |
indexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int fromIndex)
指定范围内查找字符串
|
static String |
int2Str(int data)
用缓存将int转成str
|
static boolean |
isBlank(CharSequence str)
字符串是否为空白 空白的定义如下:
1、为null 2、为不可见字符(如空格) 3、"" |
static boolean |
isEmpty(CharSequence str)
字符串是否为空,空的定义如下:
1、为null 2、为"" |
static boolean |
isNotBlank(CharSequence str)
字符串是否为非空白 空白的定义如下:
1、不为null 2、不为不可见字符(如空格) 3、不为"" |
static boolean |
isSubEquals(CharSequence str1,
int start1,
CharSequence str2,
int start2,
int length,
boolean ignoreCase)
截取两个字符串的不同部分(长度一致),判断截取的子串是否相同
任意一个字符串为null返回false |
static int |
lastIndexOf(CharSequence str,
CharSequence searchStr,
int fromIndex,
boolean ignoreCase)
指定范围内查找字符串
|
static int |
lastIndexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
指定范围内查找字符串,忽略大小写
|
static int |
lastIndexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int fromIndex)
指定范围内查找字符串,忽略大小写
|
static String |
lowerFirst(CharSequence str)
小写首字母
例如:str = Name, return name |
static String |
maxLength(CharSequence string,
int length)
限制字符串长度,如果超过指定长度,截取指定长度并在末尾加"..."
|
static String |
repeat(char c,
int count)
重复某个字符
|
static String[] |
split(String str,
String separator) |
static boolean |
startWith(CharSequence str,
char c)
字符串是否以给定字符开始
|
static boolean |
startWith(CharSequence str,
CharSequence prefix)
是否以指定字符串开头
|
static boolean |
startWith(CharSequence str,
CharSequence prefix,
boolean isIgnoreCase)
是否以指定字符串开头
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false |
static boolean |
startWithIgnoreCase(CharSequence str,
CharSequence prefix)
是否以指定字符串开头,忽略大小写
|
static String |
str(CharSequence cs)
CharSequence 转为字符串,null安全 |
static String |
str(Object obj,
Charset charset)
将对象转为字符串
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 |
static String |
str(Object obj,
String charsetName)
将对象转为字符串
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 |
static String |
sub(CharSequence str,
int fromIndex,
int toIndex)
改进JDK subString
index从0开始计算,最后一个字符为-1 如果from和to位置一样,返回 "" 如果from或to为负数,则按照length从后向前数位置,如果绝对值大于字符串长度,则from归到0,to归到length 如果经过修正的index中from大于to,则互换from和to example: abcdefgh 2 3 =》 c abcdefgh 2 -3 =》 cde |
static String |
subAfter(CharSequence string,
CharSequence separator,
boolean isLastSeparator)
截取分隔字符串之后的字符串,不包括分隔字符串
如果给定的字符串为空串(null或""),返回原字符串 如果分隔字符串为空串(null或""),则返回空串,如果分隔字符串未找到,返回空串 栗子: |
static String |
subBefore(CharSequence string,
CharSequence separator,
boolean isLastSeparator)
截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串 如果分隔字符串为空串"",则返回空串,如果分隔字符串未找到,返回原字符串 栗子: |
static String |
subBetween(CharSequence str,
CharSequence beforeAndAfter)
截取指定字符串中间部分,不包括标识字符串
栗子: |
static String |
subBetween(CharSequence str,
CharSequence before,
CharSequence after)
截取指定字符串中间部分,不包括标识字符串
栗子: |
static String |
subPre(CharSequence string,
int toIndex)
切割指定位置之前部分的字符串
|
static String |
subPreGbk(CharSequence str,
int len,
CharSequence suffix)
截取部分字符串,这里一个汉字的长度认为是2
|
static String |
subSuf(CharSequence string,
int fromIndex)
切割指定位置之后部分的字符串
|
static String |
subSufByLength(CharSequence string,
int length)
切割指定长度的后部分的字符串
|
static String |
subWithLength(String input,
int fromIndex,
int length)
截取字符串,从指定位置开始,截取指定长度的字符串
author weibaohui |
static String |
trim(String value)
去除字符串两边空白符,传入null也返回null
|
static String |
trimEnd(CharSequence str)
除去字符串尾部的空白,如果字符串是
null ,则返回null 。 |
static String |
trimStart(CharSequence str)
除去字符串头部的空白,如果字符串是
null ,则返回null 。 |
static String |
upperFirst(CharSequence str)
大写首字母
例如:str = name, return Name |
static String |
utf8Str(Object obj)
将对象转为字符串
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 |
public static final int INDEX_NOT_FOUND
public static final String EMPTY
public static String int2Str(int data)
data
- public static String trim(String value)
value
- 值public static boolean isEmpty(CharSequence str)
str
- 被检测的字符串public static boolean isNotBlank(CharSequence str)
str
- 被检测的字符串public static boolean isBlank(CharSequence str)
str
- 被检测的字符串public static String arrayToString(Object obj)
obj
- 集合或数组对象public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix)
str
- 被监测字符串prefix
- 开头字符串public static boolean startWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase)
str
- 被监测字符串prefix
- 开头字符串isIgnoreCase
- 是否忽略大小写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)
str1
- 要比较的字符串1str2
- 要比较的字符串2ignoreCase
- 是否忽略大小写null
,则返回true
public static String fillAfter(String str, char filledChar, int len)
str
- 被填充的字符串filledChar
- 填充的字符len
- 填充长度public static String fill(String str, char filledChar, int len, boolean isPre)
str
- 被填充的字符串filledChar
- 填充的字符len
- 填充长度isPre
- 是否填充在前public static String repeat(char c, int count)
c
- 被重复的字符count
- 重复的数目,如果小于等于0则返回""public static String trimStart(CharSequence str)
null
,则返回null
。
注意,和String.trim
不同,此方法使用CharUtil.isBlankChar
来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
trimStart(null) = null trimStart("") = "" trimStart("abc") = "abc" trimStart(" abc") = "abc" trimStart("abc ") = "abc " trimStart(" abc ") = "abc "
str
- 要处理的字符串null
或结果字符串为""
,则返回 null
public static String trimEnd(CharSequence str)
null
,则返回null
。
注意,和String.trim
不同,此方法使用CharUtil.isBlankChar
来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
trimEnd(null) = null trimEnd("") = "" trimEnd("abc") = "abc" trimEnd(" abc") = " abc" trimEnd("abc ") = "abc" trimEnd(" abc ") = " abc"
str
- 要处理的字符串null
或结果字符串为""
,则返回 null
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 str, char searchChar, int start, int end)
str
- 字符串searchChar
- 被查找的字符start
- 起始位置,如果小于0,从0开始查找end
- 终止位置,如果超过str.length()则默认查找到字符串末尾public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr)
StrUtil.indexOfIgnoreCase(null, *, *) = -1 StrUtil.indexOfIgnoreCase(*, null, *) = -1 StrUtil.indexOfIgnoreCase("", "", 0) = 0 StrUtil.indexOfIgnoreCase("aabaabaa", "A", 0) = 0 StrUtil.indexOfIgnoreCase("aabaabaa", "B", 0) = 2 StrUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 StrUtil.indexOfIgnoreCase("aabaabaa", "B", 3) = 5 StrUtil.indexOfIgnoreCase("aabaabaa", "B", 9) = -1 StrUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2 StrUtil.indexOfIgnoreCase("aabaabaa", "", 2) = 2 StrUtil.indexOfIgnoreCase("abc", "", 9) = -1
str
- 字符串searchStr
- 需要查找位置的字符串public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int fromIndex)
StrUtil.indexOfIgnoreCase(null, *, *) = -1 StrUtil.indexOfIgnoreCase(*, null, *) = -1 StrUtil.indexOfIgnoreCase("", "", 0) = 0 StrUtil.indexOfIgnoreCase("aabaabaa", "A", 0) = 0 StrUtil.indexOfIgnoreCase("aabaabaa", "B", 0) = 2 StrUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 StrUtil.indexOfIgnoreCase("aabaabaa", "B", 3) = 5 StrUtil.indexOfIgnoreCase("aabaabaa", "B", 9) = -1 StrUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2 StrUtil.indexOfIgnoreCase("aabaabaa", "", 2) = 2 StrUtil.indexOfIgnoreCase("abc", "", 9) = -1
str
- 字符串searchStr
- 需要查找位置的字符串fromIndex
- 起始位置public static int indexOf(CharSequence str, CharSequence searchStr, int fromIndex, boolean ignoreCase)
str
- 字符串searchStr
- 需要查找位置的字符串fromIndex
- 起始位置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 str, CharSequence searchStr, int fromIndex, boolean ignoreCase)
str
- 字符串searchStr
- 需要查找位置的字符串fromIndex
- 起始位置,从后往前计数ignoreCase
- 是否忽略大小写public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase)
str1
- 第一个字符串start1
- 第一个字符串开始的位置str2
- 第二个字符串start2
- 第二个字符串开始的位置length
- 截取长度ignoreCase
- 是否忽略大小写public static boolean startWith(CharSequence str, char c)
str
- 字符串c
- 字符public static boolean startWith(CharSequence str, CharSequence prefix)
str
- 被监测字符串prefix
- 开头字符串public static boolean endWith(CharSequence str, char c)
str
- 字符串c
- 字符public static boolean endWith(CharSequence str, CharSequence suffix, boolean isIgnoreCase)
str
- 被监测字符串suffix
- 结尾字符串isIgnoreCase
- 是否忽略大小写public static boolean endWith(CharSequence str, CharSequence suffix)
str
- 被监测字符串suffix
- 结尾字符串public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix)
str
- 被监测字符串suffix
- 结尾字符串public static boolean contains(CharSequence str, char searchChar)
str
- 字符串searchChar
- 被查找的字符public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr)
null
,返回truestr
- 被检测字符串testStr
- 被测试是否包含的字符串public static String[] split(String str, String separator)
str
- separator
- public static String utf8Str(Object obj)
obj
- 对象public static String str(Object obj, String charsetName)
obj
- 对象charsetName
- 字符集public static String str(Object obj, Charset charset)
obj
- 对象charset
- 字符集public static String str(CharSequence cs)
CharSequence
转为字符串,null安全cs
- CharSequence
public static String sub(CharSequence str, int fromIndex, int toIndex)
str
- StringfromIndex
- 开始的index(包括)toIndex
- 结束的index(不包括)public static String subPreGbk(CharSequence str, int len, CharSequence suffix)
str
- 字符串len
- 切割的位置suffix
- 切割后加上后缀public static String maxLength(CharSequence string, int length)
string
- 字符串length
- 最大长度public static String subPre(CharSequence string, int toIndex)
string
- 字符串toIndex
- 切割到的位置(不包括)public static String subSuf(CharSequence string, int fromIndex)
string
- 字符串fromIndex
- 切割开始的位置(包括)public static String subSufByLength(CharSequence string, int length)
StrUtil.subSufByLength("abcde", 3) = "cde" StrUtil.subSufByLength("abcde", 0) = "" StrUtil.subSufByLength("abcde", -5) = "" StrUtil.subSufByLength("abcde", -1) = "" StrUtil.subSufByLength("abcde", 5) = "abcde" StrUtil.subSufByLength("abcde", 10) = "abcde" StrUtil.subSufByLength(null, 3) = null
string
- 字符串length
- 切割长度public static String subWithLength(String input, int fromIndex, int length)
input
- 原始字符串fromIndex
- 开始的index,包括length
- 要截取的长度public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator)
StrUtil.subBefore(null, *) = null StrUtil.subBefore("", *) = "" StrUtil.subBefore("abc", "a") = "" StrUtil.subBefore("abcba", "b") = "a" StrUtil.subBefore("abc", "c") = "ab" StrUtil.subBefore("abc", "d") = "abc" StrUtil.subBefore("abc", "") = "" StrUtil.subBefore("abc", null) = "abc"
string
- 被查找的字符串separator
- 分隔字符串(不包括)isLastSeparator
- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator)
StrUtil.subAfter(null, *) = null StrUtil.subAfter("", *) = "" StrUtil.subAfter(*, null) = "" StrUtil.subAfter("abc", "a") = "bc" StrUtil.subAfter("abcba", "b") = "cba" StrUtil.subAfter("abc", "c") = "" StrUtil.subAfter("abc", "d") = "" StrUtil.subAfter("abc", "") = "abc"
string
- 被查找的字符串separator
- 分隔字符串(不包括)isLastSeparator
- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个public static String subBetween(CharSequence str, CharSequence before, CharSequence after)
StrUtil.subBetween("wx[b]yz", "[", "]") = "b" StrUtil.subBetween(null, *, *) = null StrUtil.subBetween(*, null, *) = null StrUtil.subBetween(*, *, null) = null StrUtil.subBetween("", "", "") = "" StrUtil.subBetween("", "", "]") = null StrUtil.subBetween("", "[", "]") = null StrUtil.subBetween("yabcz", "", "") = "" StrUtil.subBetween("yabcz", "y", "z") = "abc" StrUtil.subBetween("yabczyabcz", "y", "z") = "abc"
str
- 被切割的字符串before
- 截取开始的字符串标识after
- 截取到的字符串标识public static String subBetween(CharSequence str, CharSequence beforeAndAfter)
StrUtil.subBetween(null, *) = null StrUtil.subBetween("", "") = "" StrUtil.subBetween("", "tag") = null StrUtil.subBetween("tagabctag", null) = null StrUtil.subBetween("tagabctag", "") = "" StrUtil.subBetween("tagabctag", "tag") = "abc"
str
- 被切割的字符串beforeAndAfter
- 截取开始和结束的字符串标识public static Object convert(Class<?> type, String value) throws Exception
type
- value
- Exception
public static Object convert(Class<?> type, String[] values) throws Exception
type
- values
- Exception
public static boolean containsAny(CharSequence str, CharSequence... testStrs)
str
- 指定字符串testStrs
- 需要检查的字符串数组public static String getContainsStr(CharSequence str, CharSequence... testStrs)
str
- 指定字符串testStrs
- 需要检查的字符串数组public static String upperFirst(CharSequence str)
str
- 字符串public static String lowerFirst(CharSequence str)
str
- 字符串Copyright © 2021. All rights reserved.