public class StringUtil
extends org.springframework.util.StringUtils
| Constructor and Description |
|---|
StringUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
cleanIdentifier(String param)
获取标识符,用于参数清理
|
static String |
cleanText(String txt)
清理字符串,清理出某些不可见字符和一些sql特殊字符
|
static <T extends CharSequence> |
defaultIfBlank(T str,
T defaultStr)
Returns either the passed in CharSequence, or if the CharSequence is
whitespace, empty ("") or
null, the value of defaultStr. |
static boolean |
endWith(CharSequence cs,
char c)
endWith char
|
static String |
escapeHtml(String html)
转义HTML用于安全过滤
|
static String |
firstCharToLower(String str)
首字母变小写
|
static String |
firstCharToUpper(String str)
首字母变大写
|
static String |
format(long nanos)
格式化执行时间,单位为 ms 和 s,保留三位小数
|
static String |
format(String message,
Map<String,?> params)
将字符串中特定模式的字符转换成map中对应的值
use: format("my name is ${name}, and i like ${like}!"
|
static String |
format(String message,
Object... arguments)
同 log 格式的 format 规则
use: format("my name is {}, and i like {}!"
|
static String |
getFastId(int len)
获取一个快速生成的随机 id,包含数字,大小写,同长度比 uuid 冲突概率更小得多
|
static String |
getId(Random random,
int len)
获取一个生成的随机 id,同长度比 uuid 冲突概率更小得多
|
static String |
getId(Random random,
int len,
int radix)
获取一个生成的随机 id,同长度比 uuid 冲突概率更小得多
|
static String |
getNanoId()
一个小巧、安全、URL友好、21 位的字符串ID生成器。
|
static String |
getNanoId62()
一个小巧、安全、URL友好、21 位的字符串ID生成器,62 进制,只包含数字、大写、小写字母
|
static String |
getSafeId(int len)
获取一个安全的随机 id,包含数字,大小写,同长度比 uuid 冲突概率更小得多
|
static String |
getSafeNanoId()
一个小巧、安全、URL友好、21 位的字符串ID生成器。
|
static String |
getSafeNanoId62()
一个小巧、安全、URL友好、21 位的字符串ID生成器,62 进制,只包含数字、大写、小写字母
|
static String |
getSafeUUID()
生成安全的 uuid(冲突概率小),采用 jdk 9 的形式,优化性能
|
static String |
getUUID()
生成 uuid,随机位数是普通 uuid 的一倍,冲突概率更小
|
static boolean |
isAnyBlank(CharSequence... css)
有 任意 一个 Blank
|
static boolean |
isAnyBlank(Collection<CharSequence> css)
有 任意 一个 Blank
|
static boolean |
isAnyNotBlank(CharSequence... css)
有 任意 一个 Blank
|
static boolean |
isAnyNotBlank(Collection<CharSequence> css)
有 任意 一个 Blank
|
static boolean |
isBlank(CharSequence cs)
Check whether the given
CharSequence contains actual text. |
static boolean |
isHttpUrl(String text)
判断是否 http 地址
|
static boolean |
isNoneBlank(CharSequence... css)
是否全非 Blank
|
static boolean |
isNoneBlank(Collection<CharSequence> css)
是否全非 Blank
|
static boolean |
isNotBlank(CharSequence cs)
Checks if a CharSequence is not empty (""), not null and not whitespace only.
|
static boolean |
isNumeric(CharSequence cs)
判断一个字符串是否是数字
|
static String |
join(Collection<?> coll)
Convert a
Collection into a delimited String (e.g., CSV). |
static String |
join(Collection<?> coll,
String delim)
Convert a
Collection into a delimited String (e.g. |
static String |
join(Object[] arr)
Convert a
String array into a comma delimited String
(i.e., CSV). |
static String |
join(Object[] arr,
String delim)
Convert a
String array into a delimited String (e.g. |
static String |
left(String str,
int len)
参考自 commons lang 微调
Gets the leftmost
len characters of a String. |
static String |
leftPad(String str,
int size)
参考自 commons lang 微调
Left pad a String with spaces (' ').
|
static String |
leftPad(String str,
int size,
char padChar)
参考自 commons lang 微调
Left pad a String with a specified character.
|
static String |
leftPad(String str,
int size,
String padStr)
参考自 commons lang 微调
Left pad a String with a specified String.
|
static String |
mid(String str,
int pos,
int len)
参考自 commons lang 微调
Gets
len characters from the middle of a String. |
static String |
random(int count)
随机数生成
|
static String |
random(int count,
RandomType randomType)
随机数生成
|
static String |
repeat(char ch,
int repeat)
参考自 commons lang 微调
Returns padding using the specified delimiter repeated
to a given length.
|
static String |
right(String str,
int len)
参考自 commons lang 微调
Gets the rightmost
len characters of a String. |
static String |
rightPad(String str,
int size)
参考自 commons lang 微调
Right pad a String with spaces (' ').
|
static String |
rightPad(String str,
int size,
char padChar)
参考自 commons lang 微调
Right pad a String with a specified character.
|
static String |
rightPad(String str,
int size,
String padStr)
参考自 commons lang 微调
Right pad a String with a specified String.
|
static boolean |
simpleMatch(String[] patterns,
String str)
字符串是否符合指定的 表达式
pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy"
|
static boolean |
simpleMatch(String pattern,
String str)
字符串是否符合指定的 表达式
pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy"
|
static String[] |
split(String str,
String delimiter)
分割 字符串
|
static String[] |
splitTrim(String str,
String delimiter)
分割 字符串 删除常见 空白符
|
static boolean |
startWith(CharSequence cs,
char c)
startWith char
|
addStringToArray, applyRelativePath, arrayToCommaDelimitedString, arrayToDelimitedString, capitalize, cleanPath, collectionToCommaDelimitedString, collectionToDelimitedString, collectionToDelimitedString, commaDelimitedListToSet, commaDelimitedListToStringArray, concatenateStringArrays, containsWhitespace, containsWhitespace, countOccurrencesOf, delete, deleteAny, delimitedListToStringArray, delimitedListToStringArray, endsWithIgnoreCase, getFilename, getFilenameExtension, hasLength, hasLength, hasText, hasText, isEmpty, matchesCharacter, mergeStringArrays, parseLocale, parseLocaleString, parseTimeZoneString, pathEquals, quote, quoteIfString, removeDuplicateStrings, replace, sortStringArray, splitArrayElementsIntoProperties, splitArrayElementsIntoProperties, startsWithIgnoreCase, stripFilenameExtension, substringMatch, tokenizeToStringArray, tokenizeToStringArray, toLanguageTag, toStringArray, toStringArray, trimAllWhitespace, trimAllWhitespace, trimArrayElements, trimLeadingCharacter, trimLeadingWhitespace, trimTrailingCharacter, trimTrailingWhitespace, trimWhitespace, truncate, truncate, uncapitalize, unqualify, unqualify, uriDecodepublic static String firstCharToLower(String str)
str - 字符串public static String firstCharToUpper(String str)
str - 字符串public static boolean isBlank(@Nullable
CharSequence cs)
CharSequence contains actual text.
More specifically, this method returns true if the
CharSequence is not null, its length is greater than
0, and it contains at least one non-whitespace character.
StringUtil.isBlank(null) = true
StringUtil.isBlank("") = true
StringUtil.isBlank(" ") = true
StringUtil.isBlank("12345") = false
StringUtil.isBlank(" 12345 ") = false
cs - the CharSequence to check (may be null)true if the CharSequence is not null,
its length is greater than 0, and it does not contain whitespace onlyCharacter.isWhitespace(char)public static boolean isNotBlank(@Nullable
CharSequence cs)
Checks if a CharSequence is not empty (""), not null and not whitespace only.
StringUtil.isNotBlank(null) = false
StringUtil.isNotBlank("") = false
StringUtil.isNotBlank(" ") = false
StringUtil.isNotBlank("bob") = true
StringUtil.isNotBlank(" bob ") = true
cs - the CharSequence to check, may be nulltrue if the CharSequence is
not empty and not null and not whitespaceCharacter.isWhitespace(char)public static boolean isAnyBlank(CharSequence... css)
css - CharSequencepublic static boolean isAnyBlank(Collection<CharSequence> css)
css - CharSequencepublic static boolean isNoneBlank(CharSequence... css)
css - CharSequencepublic static boolean isNoneBlank(Collection<CharSequence> css)
css - CharSequencepublic static boolean isAnyNotBlank(CharSequence... css)
css - CharSequencepublic static boolean isAnyNotBlank(Collection<CharSequence> css)
css - CharSequencepublic static boolean isNumeric(CharSequence cs)
cs - the CharSequence to check, may be nullpublic static boolean startWith(CharSequence cs, char c)
cs - CharSequencec - charpublic static boolean endWith(CharSequence cs, char c)
cs - CharSequencec - charpublic static String format(@Nullable String message, @Nullable Map<String,?> params)
use: format("my name is ${name}, and i like ${like}!", {"name":"L.cm", "like": "Java"})
message - 需要转换的字符串params - 转换所需的键值对集合public static String format(@Nullable String message, @Nullable Object... arguments)
use: format("my name is {}, and i like {}!", "L.cm", "Java")
message - 需要转换的字符串arguments - 需要替换的变量public static String format(long nanos)
nanos - 纳秒public static String join(Collection<?> coll)
Collection into a delimited String (e.g., CSV).
Useful for toString() implementations.
coll - the Collection to convertStringpublic static String join(Collection<?> coll, String delim)
Collection into a delimited String (e.g. CSV).
Useful for toString() implementations.
coll - the Collection to convertdelim - the delimiter to use (typically a ",")Stringpublic static String join(Object[] arr)
String array into a comma delimited String
(i.e., CSV).
Useful for toString() implementations.
arr - the array to displayStringpublic static String join(Object[] arr, String delim)
String array into a delimited String (e.g. CSV).
Useful for toString() implementations.
arr - the array to displaydelim - the delimiter to use (typically a ",")Stringpublic static String[] split(@Nullable String str, @Nullable String delimiter)
str - 字符串delimiter - 分割符public static String[] splitTrim(@Nullable String str, @Nullable String delimiter)
str - 字符串delimiter - 分割符public static boolean simpleMatch(@Nullable
String pattern,
@Nullable
String str)
pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy"
pattern - 表达式str - 字符串public static boolean simpleMatch(@Nullable
String[] patterns,
String str)
pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy"
patterns - 表达式 数组str - 字符串public static String getUUID()
public static String getSafeUUID()
public static String getNanoId()
public static String getNanoId62()
public static String getSafeNanoId()
public static String getSafeNanoId62()
public static String getFastId(int len)
len - 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。public static String getSafeId(int len)
len - 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。public static String getId(Random random, int len)
random - Randomlen - 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。public static String getId(Random random, int len, int radix)
random - Randomlen - 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。radix - radix,36 包含字母和数字,62 包含大写字母public static String escapeHtml(String html)
html - html@Nullable public static String cleanText(@Nullable String txt)
txt - 文本@Nullable public static String cleanIdentifier(@Nullable String param)
param - 参数public static String random(int count)
count - 字符长度public static String random(int count, RandomType randomType)
count - 字符长度randomType - 随机数类别public static String repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.
StringUtils.repeat('e', 0) = ""
StringUtils.repeat('e', 3) = "eee"
StringUtils.repeat('e', -2) = ""
Note: this method does not support padding with
Unicode Supplementary Characters
as they require a pair of chars to be represented.
ch - character to repeatrepeat - number of times to repeat char, negative treated as zero@Nullable public static String left(@Nullable String str, int len)
Gets the leftmost len characters of a String.
If len characters are not available, or the
String is null, the String will be returned without
an exception. An empty String is returned if len is negative.
StringUtils.left(null, *) = null
StringUtils.left(*, -ve) = ""
StringUtils.left("", *) = ""
StringUtils.left("abc", 0) = ""
StringUtils.left("abc", 2) = "ab"
StringUtils.left("abc", 4) = "abc"
str - the CharSequence to get the leftmost characters from, may be nulllen - the length of the required Stringnull if null String input@Nullable public static String right(@Nullable String str, int len)
Gets the rightmost len characters of a String.
If len characters are not available, or the String
is null, the String will be returned without an
an exception. An empty String is returned if len is negative.
StringUtils.right(null, *) = null
StringUtils.right(*, -ve) = ""
StringUtils.right("", *) = ""
StringUtils.right("abc", 0) = ""
StringUtils.right("abc", 2) = "bc"
StringUtils.right("abc", 4) = "abc"
str - the String to get the rightmost characters from, may be nulllen - the length of the required Stringnull if null String input@Nullable public static String rightPad(@Nullable String str, int size)
Right pad a String with spaces (' ').
The String is padded to the size of size.
StringUtils.rightPad(null, *) = null
StringUtils.rightPad("", 3) = " "
StringUtils.rightPad("bat", 3) = "bat"
StringUtils.rightPad("bat", 5) = "bat "
StringUtils.rightPad("bat", 1) = "bat"
StringUtils.rightPad("bat", -1) = "bat"
str - the String to pad out, may be nullsize - the size to pad tonull if null String input@Nullable public static String rightPad(@Nullable String str, int size, char padChar)
Right pad a String with a specified character.
The String is padded to the size of size.
StringUtils.rightPad(null, *, *) = null
StringUtils.rightPad("", 3, 'z') = "zzz"
StringUtils.rightPad("bat", 3, 'z') = "bat"
StringUtils.rightPad("bat", 5, 'z') = "batzz"
StringUtils.rightPad("bat", 1, 'z') = "bat"
StringUtils.rightPad("bat", -1, 'z') = "bat"
str - the String to pad out, may be nullsize - the size to pad topadChar - the character to pad withnull if null String input@Nullable public static String rightPad(@Nullable String str, int size, String padStr)
Right pad a String with a specified String.
The String is padded to the size of size.
StringUtils.rightPad(null, *, *) = null
StringUtils.rightPad("", 3, "z") = "zzz"
StringUtils.rightPad("bat", 3, "yz") = "bat"
StringUtils.rightPad("bat", 5, "yz") = "batyz"
StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"
StringUtils.rightPad("bat", 1, "yz") = "bat"
StringUtils.rightPad("bat", -1, "yz") = "bat"
StringUtils.rightPad("bat", 5, null) = "bat "
StringUtils.rightPad("bat", 5, "") = "bat "
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 spacenull if null String input@Nullable public static String leftPad(@Nullable String str, int size)
Left pad a String with spaces (' ').
The String is padded to the size of size.
StringUtils.leftPad(null, *) = null
StringUtils.leftPad("", 3) = " "
StringUtils.leftPad("bat", 3) = "bat"
StringUtils.leftPad("bat", 5) = " bat"
StringUtils.leftPad("bat", 1) = "bat"
StringUtils.leftPad("bat", -1) = "bat"
str - the String to pad out, may be nullsize - the size to pad tonull if null String input@Nullable public static String leftPad(@Nullable String str, int size, char padChar)
Left pad a String with a specified character.
Pad to a size of size.
StringUtils.leftPad(null, *, *) = null
StringUtils.leftPad("", 3, 'z') = "zzz"
StringUtils.leftPad("bat", 3, 'z') = "bat"
StringUtils.leftPad("bat", 5, 'z') = "zzbat"
StringUtils.leftPad("bat", 1, 'z') = "bat"
StringUtils.leftPad("bat", -1, 'z') = "bat"
str - the String to pad out, may be nullsize - the size to pad topadChar - the character to pad withnull if null String input@Nullable public static String leftPad(@Nullable String str, int size, String padStr)
Left pad a String with a specified String.
Pad to a size of size.
StringUtils.leftPad(null, *, *) = null
StringUtils.leftPad("", 3, "z") = "zzz"
StringUtils.leftPad("bat", 3, "yz") = "bat"
StringUtils.leftPad("bat", 5, "yz") = "yzbat"
StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"
StringUtils.leftPad("bat", 1, "yz") = "bat"
StringUtils.leftPad("bat", -1, "yz") = "bat"
StringUtils.leftPad("bat", 5, null) = " bat"
StringUtils.leftPad("bat", 5, "") = " bat"
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 spacenull if null String input@Nullable public static String mid(@Nullable String str, int pos, int len)
Gets len characters from the middle of a String.
If len characters are not available, the remainder
of the String will be returned without an exception. If the
String is null, null will be returned.
An empty String is returned if len is negative or exceeds the
length of str.
StringUtils.mid(null, *, *) = null
StringUtils.mid(*, *, -ve) = ""
StringUtils.mid("", 0, *) = ""
StringUtils.mid("abc", 0, 2) = "ab"
StringUtils.mid("abc", 0, 4) = "abc"
StringUtils.mid("abc", 2, 4) = "c"
StringUtils.mid("abc", 4, 2) = ""
StringUtils.mid("abc", -2, 2) = "ab"
str - the String to get the characters from, may be nullpos - the position to start from, negative treated as zerolen - the length of the required Stringnull if null String inputpublic static boolean isHttpUrl(String text)
text - 文本@Nullable public static <T extends CharSequence> T defaultIfBlank(@Nullable T str, @Nullable T defaultStr)
null, the value of defaultStr.
Whitespace is defined by Character.isWhitespace(char).
StringUtils.defaultIfBlank(null, "NULL") = "NULL"
StringUtils.defaultIfBlank("", "NULL") = "NULL"
StringUtils.defaultIfBlank(" ", "NULL") = "NULL"
StringUtils.defaultIfBlank("bat", "NULL") = "bat"
StringUtils.defaultIfBlank("", null) = null
T - the specific kind of CharSequencestr - the CharSequence to check, may be nulldefaultStr - the default CharSequence to return
if the input is whitespace, empty ("") or null, may be nullCopyright © 2024. All rights reserved.