Class StringUtil
- Since:
- 0.0.1
- Version:
- v0.0.1.0
- Author:
- ACANX
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcamelCaseToUnderline(String camelString) 将下划线字符串转换为驼峰字符串static StringcamelToSplitName(String camelName, String split) 驼峰转下划线static booleancontains(CharSequence seq, CharSequence searchSeq) Checks if CharSequence contains a search CharSequence, handlingnull.static booleancontainsAll(String str, String searchChars) containsAllstatic booleancontainsString(String inputString, List<String> stringList) 判断输入的字符串是否是给定字符串列表中的某一个等值的字符串static StringdefaultIfBlank(String str) defaultIfBlankstatic boolean字符串相同判断static DateformattedDateStringToDate(String dateStr, String dateFormat) 将String字符串转换为java.util.Date格式日期static TimestampformattedDateStringToTimestamp(String dateStr, String format) 日期时间字符串转Timestampstatic TimestampformattedDateStrToSqlDate(String dateStr, String dateFormat) 将String字符串转换为java.sql.Timestamp格式日期,用于数据库保存static LongformattedTimeStringToLong(String time, String format) 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ssstatic String获取当前日期时间字符串static StringgetStringMD5Code(String str) 字符串的MD5加密 默认输出为大写十六进制字符static StringgetStringSHA1Code(String input) 获取字符串的SHA1值static String生成指定长度的指定字符串 如: getStrSeq("123", 3) return "123123123" getStrSeq("321", 4) return "321321321321"static booleanisAlpha(CharSequence cs) Checks if the CharSequence contains only Unicode letters.static booleanisAlphanumeric(String str) StringUtil.isAlphanumeric(null) = false StringUtil.isAlphanumeric("") = false StringUtil.isAlphanumeric(" ") = false StringUtil.isAlphanumeric("abc") = true StringUtil.isAlphanumeric("ab c") = false StringUtil.isAlphanumeric("ab2c") = true StringUtil.isAlphanumeric("ab-c") = falsestatic boolean字符串空格判断static booleanisEmpty(CharSequence cs) Checks if a CharSequence is empty ("") or null.static booleanChecks if a CharSequence is empty ("") or null.static booleanisNotBlank(String str) StringUtil.isNotBlank(null) = false StringUtil.isNotBlank("") = false StringUtil.isNotBlank(" ") = false StringUtil.isNotBlank("bob") = true StringUtil.isNotBlank(" bob ") = truestatic booleanisNotEmpty(String str) Checks if a CharSequence is not empty ("") and not null.static booleanStringUtil.isNumeric(null) = false StringUtil.isNumeric("") = false StringUtil.isNumeric(" ") = false StringUtil.isNumeric("123") = true StringUtil.isNumeric("१२३") = true StringUtil.isNumeric("12 3") = false StringUtil.isNumeric("ab2c") = false StringUtil.isNumeric("12-3") = false StringUtil.isNumeric("12.3") = false StringUtil.isNumeric("-123") = false StringUtil.isNumeric("+123") = falsestatic booleanChecks if the CharSequence contains only whitespace.static Stringjoin(int[] array, char separator) Joins the elements of the provided array into a single String containing the provided list of elements.static Stringjoin(int[] array, char delimiter, int startIndex, int endIndex) Joins the elements of the provided array into a single String containing the provided list of elements.static Stringjoinstatic StringJoins the elements of the provided array into a single String containing the provided list of elements.static StringJoins the elements of the provided array into a single String containing the provided list of elements.static StringJoins the elements of the providedIteratorinto a single String containing the provided elements.static StringJoins the elements of the providedListinto a single String containing the provided list of elements.static <T> Stringjoin(T... elements) Joins the elements of the provided array into a single String containing the provided list of elements.static Stringleftstatic Stringleftstatic StringleftDelChar(String descStr, String text, char ch) leftDelCharstatic StringLeft pad a String with a specified character.static StringLeft pad a String with a specified String.static StringlistToString(List<String> list, boolean wrappeFlag, char separator) 集合转字符串(以separator(如逗号)间隔)static intpositionOf(String str, char searchChar) 字符在字符串中的位置static intpositionOf(String str, String searchStr) 子字符串在字符串中的位置static String字符串左static Stringrightstatic StringRight pad a String with a specified character.static StringRight pad a String with a specified String.static StringsplitNameByLastCamel(String camelName) splitNameByLastCamelstringToList(String str) 元素之间以逗号间隔的字符串转集合static StringGets a substring from the specified String avoiding exceptions.static Stringsubstringstatic booleansubstringEquals(String str1, int pos, int len, String str2) substringEqualsstatic StringtimestampStrToFormattedDateString(String seconds, String format) 秒级的时间戳转Datestatic StringtoUpperCaseFirstChar(String value) 字符串首字母转大写static StringunderlineToCamelCase(String underlineString) 将下划线字符串转换为驼峰字符串
-
Method Details
-
isEmpty
Checks if a CharSequence is empty ("") or null.
StringUtil.isEmpty(null) = true StringUtil.isEmpty("") = true StringUtil.isEmpty(" ") = false StringUtil.isEmpty("bob") = false StringUtil.isEmpty(" bob ") = false- Parameters:
str- 字符串- Returns:
- 空值判断结果
- Since:
- 0.0.1.10
-
isNotEmpty
Checks if a CharSequence is not empty ("") and not null.
StringUtil.isNotEmpty(null) = false StringUtil.isNotEmpty("") = false StringUtil.isNotEmpty(" ") = true StringUtil.isNotEmpty("bob") = true StringUtil.isNotEmpty(" bob ") = true- Parameters:
str- 字符串- Returns:
- 判断结果
- Since:
- 0.0.1.10
-
isBlank
字符串空格判断- Parameters:
str- 字符串- Returns:
- 判断结果
- Since:
- 0.0.1.10
-
isNotBlank
StringUtil.isNotBlank(null) = false StringUtil.isNotBlank("") = false StringUtil.isNotBlank(" ") = false StringUtil.isNotBlank("bob") = true StringUtil.isNotBlank(" bob ") = true- Parameters:
str- 字符串- Returns:
- 非空格判断结果
- Since:
- 0.0.1.10
-
isWhitespace
Checks if the CharSequence contains only whitespace.
Whitespace is defined by
Character.isWhitespace(char).nullwill returnfalse. An empty CharSequence (length()=0) will returntrue.StringUtil.isWhitespace(null) = false StringUtil.isWhitespace("") = true StringUtil.isWhitespace(" ") = true StringUtil.isWhitespace("abc") = false StringUtil.isWhitespace("ab2c") = false StringUtil.isWhitespace("ab-c") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains whitespace, and is non-null- Since:
- 2.0, 3.0 Changed signature from isWhitespace(String) to isWhitespace(CharSequence)
-
equals
字符串相同判断- Parameters:
str1- 字符串1str2- 字符串2- Returns:
- 判断结果
- Since:
- 0.0.1.10
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.No delimiter is added before or after the list. A
nullseparator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.StringUtil.join(null, *) = null StringUtil.join([], *) = "" StringUtil.join([null], *) = "" StringUtil.join(["a", "b", "c"], "--") = "a--b--c" StringUtil.join(["a", "b", "c"], null) = "abc" StringUtil.join(["a", "b", "c"], "") = "abc" StringUtil.join([null, "", "a"], ',') = ",,a"
- Parameters:
array- the array of values to join together, may be nulldelimiter- the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null array input
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. A
nullseparator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.StringUtil.join(null, *, *, *) = null StringUtil.join([], *, *, *) = "" StringUtil.join([null], *, *, *) = "" StringUtil.join(["a", "b", "c"], "--", 0, 3) = "a--b--c" StringUtil.join(["a", "b", "c"], "--", 1, 3) = "b--c" StringUtil.join(["a", "b", "c"], "--", 2, 3) = "c" StringUtil.join(["a", "b", "c"], "--", 2, 2) = "" StringUtil.join(["a", "b", "c"], null, 0, 3) = "abc" StringUtil.join(["a", "b", "c"], "", 0, 3) = "abc" StringUtil.join([null, "", "a"], ',', 0, 3) = ",,a"
- Parameters:
array- the array of values to join together, may be nulldelimiter- the separator character to use, null treated as ""startIndex- the first index to start joining from.endIndex- the index to stop joining from (exclusive).- Returns:
- the joined String,
nullif null array input; or the empty string ifendIndex - startIndex <= 0. The number of joined entries is given byendIndex - startIndex - Throws:
ArrayIndexOutOfBoundsException- ife
startIndex < 0or
startIndex >= array.length()or
endIndex < 0or
endIndex > array.length()
-
join
join- Parameters:
array- arraydelimiter- delimiterstartIndex- 起始位置endIndex- 结束位置- Returns:
- 结果
-
join
Joins the elements of the provided
Listinto a single String containing the provided list of elements.No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
StringUtil.join(null, *) = null StringUtil.join([], *) = "" StringUtil.join([null], *) = "" StringUtil.join(["a", "b", "c"], ';') = "a;b;c" StringUtil.join(["a", "b", "c"], null) = "abc" StringUtil.join([null, "", "a"], ';') = ";;a"
- Parameters:
list- theListof values to join together, may be nullseparator- the separator character to usestartIndex- the first index to start joining from. It is an error to pass in a start index past the end of the listendIndex- the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the list- Returns:
- the joined String,
nullif null list input - Since:
- 3.8
-
join
Joins the elements of the provided
Iteratorinto a single String containing the provided elements.No delimiter is added before or after the list. A
nullseparator is the same as an empty String ("").See the examples here:
join(Object[],String).- Parameters:
iterator- theIteratorof values to join together, may be nullseparator- the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null iterator input
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
StringUtil.join(null, *) = null StringUtil.join([], *) = "" StringUtil.join([null], *) = "" StringUtil.join([1, 2, 3], ';') = "1;2;3" StringUtil.join([1, 2, 3], null) = "123"
- Parameters:
array- the array of values to join together, may be nullseparator- the separator character to use- Returns:
- the joined String,
nullif null array input - Since:
- 3.2
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
StringUtil.join(null, *) = null StringUtil.join([], *) = "" StringUtil.join([null], *) = "" StringUtil.join([1, 2, 3], ';') = "1;2;3" StringUtil.join([1, 2, 3], null) = "123"
- Parameters:
array- the array of values to join together, may be nulldelimiter- the separator character to usestartIndex- the first index to start joining from. It is an error to pass in a start index past the end of the arrayendIndex- the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the array- Returns:
- the joined String,
nullif null array input - Since:
- 3.2
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.
No separator is added to the joined String. Null objects or empty strings within the array are represented by empty strings.
StringUtil.join(null) = null StringUtil.join([]) = "" StringUtil.join([null]) = "" StringUtil.join(["a", "b", "c"]) = "abc" StringUtil.join([null, "", "a"]) = "a"
- Type Parameters:
T- the specific type of values to join together- Parameters:
elements- the values to join together, may be null- Returns:
- the joined String,
nullif null array input - Since:
- 2.0, 3.0 Changed signature to use varargs
-
isNumeric
StringUtil.isNumeric(null) = false StringUtil.isNumeric("") = false StringUtil.isNumeric(" ") = false StringUtil.isNumeric("123") = true StringUtil.isNumeric("१२३") = true StringUtil.isNumeric("12 3") = false StringUtil.isNumeric("ab2c") = false StringUtil.isNumeric("12-3") = false StringUtil.isNumeric("12.3") = false StringUtil.isNumeric("-123") = false StringUtil.isNumeric("+123") = false- Parameters:
str- 字符串- Returns:
- 判断结果
-
isAlpha
Checks if the CharSequence contains only Unicode letters.
nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.StringUtil.isAlpha(null) = false StringUtil.isAlpha("") = false StringUtil.isAlpha(" ") = false StringUtil.isAlpha("abc") = true StringUtil.isAlpha("ab2c") = false StringUtil.isAlpha("ab-c") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains letters, and is non-null- Since:
- 3.0 Changed signature from isAlpha(String) to isAlpha(CharSequence), 3.0 Changed "" to return false and not true
-
isEmpty
Checks if a CharSequence is empty ("") or null.
StringUtil.isEmpty(null) = true StringUtil.isEmpty("") = true StringUtil.isEmpty(" ") = false StringUtil.isEmpty("bob") = false StringUtil.isEmpty(" bob ") = falseNOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null- Since:
- 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
-
isAlphanumeric
StringUtil.isAlphanumeric(null) = false StringUtil.isAlphanumeric("") = false StringUtil.isAlphanumeric(" ") = false StringUtil.isAlphanumeric("abc") = true StringUtil.isAlphanumeric("ab c") = false StringUtil.isAlphanumeric("ab2c") = true StringUtil.isAlphanumeric("ab-c") = false- Parameters:
str- 字符串- Returns:
- 判断结果
-
getStringSHA1Code
获取字符串的SHA1值- Parameters:
input- 字符串- Returns:
- SHA1
- Since:
- 0.0.1.10
-
getStringMD5Code
字符串的MD5加密 默认输出为大写十六进制字符- Parameters:
str- 待加密的字符串- Returns:
- 加密后的MD5值,32位(十六进制字符、大写)
-
getStrSeq
生成指定长度的指定字符串 如: getStrSeq("123", 3) return "123123123" getStrSeq("321", 4) return "321321321321"- Parameters:
str- String 指定字符串length- int 循环长度- Returns:
- String 生成的字符串
-
underlineToCamelCase
将下划线字符串转换为驼峰字符串- Parameters:
underlineString- 下划线字符串- Returns:
- 小驼峰格式的字符串
-
camelCaseToUnderline
将下划线字符串转换为驼峰字符串- Parameters:
camelString- 小驼峰字符串- Returns:
- 下划线格式的字符串
-
camelToSplitName
驼峰转下划线- Parameters:
camelName- 驼峰命名字符串split- split- Returns:
- 转换后的结果字符串
-
splitNameByLastCamel
splitNameByLastCamel- Parameters:
camelName- 驼峰格式的字符串- Returns:
- 转换后的结果
-
toUpperCaseFirstChar
字符串首字母转大写- Parameters:
value- 字符串- Returns:
- 转换后的结果字符串
-
right
字符串左- Parameters:
str- 字符串num- num- Returns:
- 处理后的结果
-
right
right- Parameters:
str1- 字符串num- numstr2- str2- Returns:
- 处理结果
-
left
left- Parameters:
str- 字符串num- num- Returns:
- 处理后的结果
-
left
left- Parameters:
str1- 字符串1num- numstr2- 字符串2- Returns:
- 处理结果
-
defaultIfBlank
defaultIfBlank- Parameters:
str- 字符串- Returns:
- 处理结果
-
leftDelChar
leftDelChar- Parameters:
descStr- descStrtext- textch- 字符- Returns:
- 结果
-
rightPad
Right pad a String with a specified character.
The String is padded to the size of
size.StringUtil.rightPad(null, *, *) = null StringUtil.rightPad("", 3, 'z') = "zzz" StringUtil.rightPad("bat", 3, 'z') = "bat" StringUtil.rightPad("bat", 5, 'z') = "batzz" StringUtil.rightPad("bat", 1, 'z') = "bat" StringUtil.rightPad("bat", -1, 'z') = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadChar- the character to pad with- Returns:
- right padded String or original String if no padding is necessary,
nullif null String input - Since:
- 0.0.1.10
-
rightPad
Right pad a String with a specified String.
The String is padded to the size of
size.StringUtil.rightPad(null, *, *) = null StringUtil.rightPad("", 3, "z") = "zzz" StringUtil.rightPad("bat", 3, "yz") = "bat" StringUtil.rightPad("bat", 5, "yz") = "batyz" StringUtil.rightPad("bat", 8, "yz") = "batyzyzy" StringUtil.rightPad("bat", 1, "yz") = "bat" StringUtil.rightPad("bat", -1, "yz") = "bat" StringUtil.rightPad("bat", 5, null) = "bat " StringUtil.rightPad("bat", 5, "") = "bat "- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- right padded String or original String if no padding is necessary,
nullif null String input
-
leftPad
Left pad a String with a specified character.
Pad to a size of
size.StringUtil.leftPad(null, *, *) = null StringUtil.leftPad("", 3, 'z') = "zzz" StringUtil.leftPad("bat", 3, 'z') = "bat" StringUtil.leftPad("bat", 5, 'z') = "zzbat" StringUtil.leftPad("bat", 1, 'z') = "bat" StringUtil.leftPad("bat", -1, 'z') = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadChar- the character to pad with- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input - Since:
- 2.0
-
leftPad
Left pad a String with a specified String.
Pad to a size of
size.StringUtil.leftPad(null, *, *) = null StringUtil.leftPad("", 3, "z") = "zzz" StringUtil.leftPad("bat", 3, "yz") = "bat" StringUtil.leftPad("bat", 5, "yz") = "yzbat" StringUtil.leftPad("bat", 8, "yz") = "yzyzybat" StringUtil.leftPad("bat", 1, "yz") = "bat" StringUtil.leftPad("bat", -1, "yz") = "bat" StringUtil.leftPad("bat", 5, null) = " bat" StringUtil.leftPad("bat", 5, "") = " bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input
-
positionOf
字符在字符串中的位置- Parameters:
str- 字符串searchChar- 搜索字符- Returns:
- 位置结果
-
positionOf
子字符串在字符串中的位置- Parameters:
str- 字符串searchStr- 子字符串- Returns:
- 位置结果
-
containsAll
containsAll- Parameters:
str- strsearchChars- searchChars- Returns:
- 结果
-
contains
Checks if CharSequence contains a search CharSequence, handling
null. This method usesString.indexOf(String)if possible.A
nullCharSequence will returnfalse.StringUtil.contains(null, *) = false StringUtil.contains(*, null) = false StringUtil.contains("", "") = true StringUtil.contains("abc", "") = true StringUtil.contains("abc", "a") = true StringUtil.contains("abc", "z") = false- Parameters:
seq- 被检索的字符串searchSeq- 检索的子字符串- Returns:
- 结果
-
substring
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end
ncharacters from the end of the String.The returned substring starts with the character in the
startposition and ends before theendposition. All position counting is zero-based -- i.e., to start at the beginning of the string usestart = 0. Negative start and end positions can be used to specify offsets relative to the end of the String.If
startis not strictly to the left ofend, "" is returned.StringUtil.substring(null, *, *) = null StringUtil.substring("", * , *) = ""; StringUtil.substring("abc", 0, 2) = "ab" StringUtil.substring("abc", 2, 0) = "" StringUtil.substring("abc", 2, 4) = "c" StringUtil.substring("abc", 4, 6) = "" StringUtil.substring("abc", 2, 2) = "" StringUtil.substring("abc", -2, -1) = "b" StringUtil.substring("abc", -4, 2) = "ab"- Parameters:
str- the String to get the substring from, may be nullstart- the position to start from, negative means count back from the end of the String by this many charactersend- the position to end at (exclusive), negative means count back from the end of the String by this many characters- Returns:
- substring from start position to end position,
nullif null String input
-
substring
substring- Parameters:
str1- 字符串pos- 起始位置len- 长度str2- 字符串2- Returns:
- 结果
-
substringEquals
substringEquals- Parameters:
str1- str1pos- 位置len- 长度str2- str2- Returns:
- 结果
-
listToString
集合转字符串(以separator(如逗号)间隔)- Parameters:
list- 字符串集合wrappeFlag- 首末是否需要添加间隔符separator- 间隔符- Returns:
- 结果字符串
-
stringToList
元素之间以逗号间隔的字符串转集合- Parameters:
str- 字符串- Returns:
- 字符串集合
-
getCurrentDateTimeString
获取当前日期时间字符串- Parameters:
sdf- SimpleDateFormat- Returns:
- 格式化后的当前日期时间字符串
-
formattedDateStringToDate
将String字符串转换为java.util.Date格式日期- Parameters:
dateStr- 表示日期的字符串dateFormat- 传入字符串的日期表示格式(如:"yyyy-MM-dd HH:mm:ss")- Returns:
- java.util.Date类型日期对象(如果转换失败则返回null)
-
formattedDateStrToSqlDate
将String字符串转换为java.sql.Timestamp格式日期,用于数据库保存- Parameters:
dateStr- 表示日期的字符串dateFormat- 传入字符串的日期表示格式(如:"yyyy-MM-dd HH:mm:ss")- Returns:
- java.sql.Timestamp类型日期对象(如果转换失败则返回null)
-
formattedDateStringToTimestamp
日期时间字符串转Timestamp- Parameters:
dateStr- 日期字符串format- 格式化模板- Returns:
- Timestamp
-
formattedTimeStringToLong
将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss- Parameters:
time- 时间format- 时间格式化模式- Returns:
- Long类型的时间戳
-
timestampStrToFormattedDateString
秒级的时间戳转Date- Parameters:
seconds- 秒级的时间戳format- 格式化- Returns:
- 日期时间字符串
-
containsString
判断输入的字符串是否是给定字符串列表中的某一个等值的字符串- Parameters:
inputString- 需要判断的字符串stringList- 字符串列表- Returns:
- 如果输入的字符串在列表中,则返回 true;否则返回 false
-