Class StringUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final charstatic final Stringstatic final String[]static final Stringstatic final charstatic final Stringstatic final charstatic final intstatic final Stringstatic final charstatic final Stringstatic final charstatic final Stringstatic final char -
Method Summary
Modifier and TypeMethodDescriptionstatic StringarrayToDelimitedString(Object[] arr, String delim) static StringcamelToSplitName(String camelName, String split) static StringconvertToSplitName(String str, String split) Convert camelCase or snake_case/SNAKE_CASE to kebab-casestatic bytedecodeHexByte(CharSequence s, int pos) Decode a 2-digit hex byte from within a string.static intdecodeHexNibble(char c) static StringReturns the default string if the input string is empty, otherwise returns the input string itselfstatic Stringstatic String[]delimitedListToStringArray(String str, String delimiter) static String[]delimitedListToStringArray(String str, String delimiter, String charsToDelete) static StringencodeParameters(Map<String, String> params) Encode parameters map to string, like '[{a:b},{c:d}]'static StringgetQueryStringValue(String qs, String key) static StringgetServiceKey(Map<String, String> ps) static booleanhasText(CharSequence cs) Check the cs String whether contains non whitespace characters.static booleanisAllUpperCase(String str) static booleanisAnyEmpty(String... ss) Checks if the strings contain at least on empty or null element.static booleanisBlank(CharSequence cs) static booleanisContains(String[] values, String value) static booleanisContains(String str, char ch) static booleanisContains(String values, String value) static booleanis empty string.static booleanif s1 is null and s2 is null, then return truestatic booleanReturns true if s is a legal Java identifier.static booleanisNoneEmpty(String... ss) Checks if the strings contain empty or null elements.static booleanis not blank string.static booleanisNotContains(String str, char ch) static booleanisNotEmpty(String str) is not empty string.static booleanis positive integer or zero string.static booleanstatic Stringstatic Stringjoin string.static Stringjoin string like javascript.static Stringjoin string like javascript.static Stringjoin(Collection<String> coll, String split) static intlength(CharSequence cs) Gets a CharSequence length or0if the CharSequence isnull.static StringnullSafeToString(Object obj) static intparseInteger(String str) parse str to Integer(if str is not number or n invalid input: '<' 0, then return 0)static longparse str to Long(if str is not number or n invalid input: '<' 0, then return 0)parseParameters(String rawParameters) Decode parameters string to mapparse query string to Parameters.static StringRemoves a substring only if it is at the end of a source string, otherwise returns the source string.static Stringrepeat(char ch, int repeat) Returns padding using the specified delimiter repeated to a given length.static StringRepeat a Stringrepeattimes to form a new String.static StringRepeat a Stringrepeattimes to form a new String, with a String separator injected each time.static StringReplaces all occurrences of a String within another String.static StringReplaces a String with another String inside a larger String, for the firstmaxvalues of the search String.static StringsnakeToSplitName(String snakeName, String split) Convert snake_case or SNAKE_CASE to kebab-case.static String[]split.splitToList(String str, char ch) Splits String around matches of the given character.splitToSet(String value, char separatorChar) Split the specified value to be aSetsplitToSet(String value, char separatorChar, boolean trimElements) Split the specified value to be aSetstatic booleanstartsWithIgnoreCase(String str, String prefix) Test str whether starts with the prefix ignore case.static StringStrips any of a set of characters from the end of a String.static StringGets a substring from the specified String avoiding exceptions.static StringsubstringAfter(String str, int separator) Gets the substring after the first occurrence of a separator.static StringsubstringAfterLast(String str, int separator) Gets the substring after the last occurrence of a separator.static StringsubstringBefore(String str, int separator) Gets the substring before the first occurrence of a separator.static StringsubstringBeforeLast(String str, int separator) Gets the substring before the last occurrence of a separator.static StringtoArgumentString(Object[] args) static BooleanConverts string to Boolean based on common boolean representations.static booleanstatic StringtoCommaDelimitedString(String one, String... others) Creates a comma-delimited string from one or more string values.static String[]Tokenize the given String into a String array.tokenizeToList(String str, char... separators) Splits a string into a list of tokens using specified separators, trimming whitespace and ignoring empty tokens.static StringtoOSStyleKey(String key) static StringtoQueryString(Map<String, String> ps) static Stringstatic Stringstatic String[]toStringArray(Collection<String> collection) static Stringstatic Stringtranslate.static String
-
Field Details
-
EMPTY_STRING
- See Also:
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUND- See Also:
-
EMPTY_STRING_ARRAY
-
EQUAL_CHAR
public static final char EQUAL_CHAR- Since:
- 2.7.5
- See Also:
-
EQUAL
-
AND_CHAR
public static final char AND_CHAR- See Also:
-
AND
-
SEMICOLON_CHAR
public static final char SEMICOLON_CHAR- See Also:
-
SEMICOLON
-
QUESTION_MASK_CHAR
public static final char QUESTION_MASK_CHAR- See Also:
-
QUESTION_MASK
-
SLASH_CHAR
public static final char SLASH_CHAR- See Also:
-
SLASH
-
HYPHEN_CHAR
public static final char HYPHEN_CHAR- See Also:
-
HYPHEN
-
-
Method Details
-
length
Gets a CharSequence length or0if the CharSequence isnull.- Parameters:
cs- a CharSequence ornull- Returns:
- CharSequence length or
0if the CharSequence isnull.
-
repeat
Repeat a String
repeattimes to form a new String.StringUtils.repeat(null, 2) = null StringUtils.repeat("", 0) = "" StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = ""- Parameters:
str- the String to repeat, may be nullrepeat- number of times to repeat str, negative treated as zero- Returns:
- a new String consisting of the original String repeated,
nullif null String input
-
repeat
Repeat a String
repeattimes to form a new String, with a String separator injected each time.StringUtils.repeat(null, null, 2) = null StringUtils.repeat(null, "x", 2) = null StringUtils.repeat("", null, 0) = "" StringUtils.repeat("", "", 2) = "" StringUtils.repeat("", "x", 3) = "xxx" StringUtils.repeat("?", ", ", 3) = "?, ?, ?"- Parameters:
str- the String to repeat, may be nullseparator- the String to inject, may be nullrepeat- number of times to repeat str, negative treated as zero- Returns:
- a new String consisting of the original String repeated,
nullif null String input - Since:
- 2.5
-
removeEnd
Removes a substring only if it is at the end of a source string, otherwise returns the source string.
A
nullsource string will returnnull. An empty ("") source string will return the empty string. Anullsearch string will return the source string.StringUtils.removeEnd(null, *) = null StringUtils.removeEnd("", *) = "" StringUtils.removeEnd(*, null) = * StringUtils.removeEnd("www.domain.com", ".com.") = "www.domain.com" StringUtils.removeEnd("www.domain.com", ".com") = "www.domain" StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com" StringUtils.removeEnd("abc", "") = "abc"- Parameters:
str- the source String to search, may be nullremove- the String to search for and remove, may be null- Returns:
- the substring with the string removed if found,
nullif null String input
-
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 doesn't not support padding with Unicode Supplementary Characters as they require a pair of
chars to be represented. If you are needing to support full I18N of your applications consider usingrepeat(String, int)instead.- Parameters:
ch- character to repeatrepeat- number of times to repeat char, negative treated as zero- Returns:
- String with repeated character
- See Also:
-
stripEnd
Strips any of a set of characters from the end of a String.
A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char).StringUtils.stripEnd(null, *) = null StringUtils.stripEnd("", *) = "" StringUtils.stripEnd("abc", "") = "abc" StringUtils.stripEnd("abc", null) = "abc" StringUtils.stripEnd(" abc", null) = " abc" StringUtils.stripEnd("abc ", null) = "abc" StringUtils.stripEnd(" abc ", null) = " abc" StringUtils.stripEnd(" abcyx", "xyz") = " abc" StringUtils.stripEnd("120.00", ".0") = "12"- Parameters:
str- the String to remove characters from, may be nullstripChars- the set of characters to remove, null treated as whitespace- Returns:
- the stripped String,
nullif null String input
-
replace
Replaces all occurrences of a String within another String.
A
nullreference passed to this method is a no-op.StringUtils.replace(null, *, *) = null StringUtils.replace("", *, *) = "" StringUtils.replace("any", null, *) = "any" StringUtils.replace("any", *, null) = "any" StringUtils.replace("any", "", *) = "any" StringUtils.replace("aba", "a", null) = "aba" StringUtils.replace("aba", "a", "") = "b" StringUtils.replace("aba", "a", "z") = "zbz"- Parameters:
text- text to search and replace in, may be nullsearchString- the String to search for, may be nullreplacement- the String to replace it with, may be null- Returns:
- the text with any replacements processed,
nullif null String input - See Also:
-
replace
Replaces a String with another String inside a larger String, for the first
maxvalues of the search String.A
nullreference passed to this method is a no-op.StringUtils.replace(null, *, *, *) = null StringUtils.replace("", *, *, *) = "" StringUtils.replace("any", null, *, *) = "any" StringUtils.replace("any", *, null, *) = "any" StringUtils.replace("any", "", *, *) = "any" StringUtils.replace("any", *, *, 0) = "any" StringUtils.replace("abaa", "a", null, -1) = "abaa" StringUtils.replace("abaa", "a", "", -1) = "b" StringUtils.replace("abaa", "a", "z", 0) = "abaa" StringUtils.replace("abaa", "a", "z", 1) = "zbaa" StringUtils.replace("abaa", "a", "z", 2) = "zbza" StringUtils.replace("abaa", "a", "z", -1) = "zbzz"- Parameters:
text- text to search and replace in, may be nullsearchString- the String to search for, may be nullreplacement- the String to replace it with, may be nullmax- maximum number of values to replace, or-1if no maximum- Returns:
- the text with any replacements processed,
nullif null String input
-
isBlank
-
isNotBlank
is not blank string.- Parameters:
cs- source string.- Returns:
- is not blank.
-
hasText
Check the cs String whether contains non whitespace characters.- Parameters:
cs-- Returns:
-
isEmpty
is empty string.- Parameters:
str- source string.- Returns:
- is empty.
-
isNoneEmpty
Checks if the strings contain empty or null elements.
StringUtils.isNoneEmpty(null) = false StringUtils.isNoneEmpty("") = false StringUtils.isNoneEmpty(" ") = true StringUtils.isNoneEmpty("abc") = true StringUtils.isNoneEmpty("abc", "def") = true StringUtils.isNoneEmpty("abc", null) = false StringUtils.isNoneEmpty("abc", "") = false StringUtils.isNoneEmpty("abc", " ") = true- Parameters:
ss- the strings to check- Returns:
trueif all strings are not empty or null
-
isAnyEmpty
Checks if the strings contain at least on empty or null element.
StringUtils.isAnyEmpty(null) = true StringUtils.isAnyEmpty("") = true StringUtils.isAnyEmpty(" ") = false StringUtils.isAnyEmpty("abc") = false StringUtils.isAnyEmpty("abc", "def") = false StringUtils.isAnyEmpty("abc", null) = true StringUtils.isAnyEmpty("abc", "") = true StringUtils.isAnyEmpty("abc", " ") = false- Parameters:
ss- the strings to check- Returns:
trueif at least one in the strings is empty or null
-
isNotEmpty
is not empty string.- Parameters:
str- source string.- Returns:
- is not empty.
-
isEquals
if s1 is null and s2 is null, then return true- Parameters:
s1- str1s2- str2- Returns:
- equals
-
isNumber
is positive integer or zero string.- Parameters:
str- a string- Returns:
- is positive integer or zero
-
parseInteger
parse str to Integer(if str is not number or n invalid input: '<' 0, then return 0)- Parameters:
str- a number str- Returns:
- positive integer or zero
-
parseLong
parse str to Long(if str is not number or n invalid input: '<' 0, then return 0)- Parameters:
str- a number str- Returns:
- positive long or zero
-
isJavaIdentifier
Returns true if s is a legal Java identifier. -
isContains
-
isContains
-
isNotContains
-
isContains
- Parameters:
values-value-- Returns:
- contains
-
isNumeric
-
toString
- Parameters:
e-- Returns:
- string
-
toString
- Parameters:
msg-e-- Returns:
- string
-
translate
translate.- Parameters:
src- source string.from- src char table.to- target char table.- Returns:
- String.
-
split
split.- Parameters:
ch- char.- Returns:
- string array.
-
splitToList
Splits String around matches of the given character.Note: Compare with
split(String, char), this method reduce memory copy. -
splitToSet
Split the specified value to be aSet- Parameters:
value- the content to be splitseparatorChar- a char to separate- Returns:
- non-null read-only
Set - Since:
- 2.7.8
-
splitToSet
Split the specified value to be aSet- Parameters:
value- the content to be splitseparatorChar- a char to separatetrimElements- require to trim the elements or not- Returns:
- non-null read-only
Set - Since:
- 2.7.8
-
join
join string.- Parameters:
array- String array.- Returns:
- String.
-
join
join string like javascript.- Parameters:
array- String array.split- split- Returns:
- String.
-
join
join string like javascript.- Parameters:
array- String array.split- split- Returns:
- String.
-
join
-
join
-
getQueryStringValue
-
parseQueryString
parse query string to Parameters.- Parameters:
qs- query string.- Returns:
- Parameters instance.
-
getServiceKey
-
toQueryString
-
camelToSplitName
-
snakeToSplitName
Convert snake_case or SNAKE_CASE to kebab-case.NOTE: Return itself if it's not a snake case.
- Parameters:
snakeName-split-- Returns:
-
convertToSplitName
Convert camelCase or snake_case/SNAKE_CASE to kebab-case- Parameters:
str-split-- Returns:
-
toArgumentString
-
trim
-
toURLKey
-
toOSStyleKey
-
isAllUpperCase
-
delimitedListToStringArray
-
delimitedListToStringArray
-
arrayToDelimitedString
-
deleteAny
-
toStringArray
-
nullSafeToString
-
parseParameters
Decode parameters string to map- Parameters:
rawParameters- format like '[{a:b},{c:d}]'- Returns:
-
encodeParameters
Encode parameters map to string, like '[{a:b},{c:d}]'- Parameters:
params-- Returns:
-
decodeHexNibble
public static int decodeHexNibble(char c) -
decodeHexByte
Decode a 2-digit hex byte from within a string. -
toCommaDelimitedString
Creates a comma-delimited string from one or more string values.- Parameters:
one- the first string valueothers- additional string values- Returns:
- the combined string, or null if the first value is null
- Since:
- 2.7.8
-
startsWithIgnoreCase
Test str whether starts with the prefix ignore case. -
defaultIf
Returns the default string if the input string is empty, otherwise returns the input string itself -
substring
Gets a substring from the specified String avoiding exceptions. If end index is not found, returns substring from start to the end -
substringBefore
Gets the substring before the first occurrence of a separator.If nothing is found, returns the original string
-
substringAfter
Gets the substring after the first occurrence of a separator.If nothing is found, the empty string is returned.
-
substringBeforeLast
Gets the substring before the last occurrence of a separator.If nothing is found, returns the original string
-
substringAfterLast
Gets the substring after the last occurrence of a separator.If nothing is found, the empty string is returned.
-
tokenize
Tokenize the given String into a String array. Trims tokens and omits empty tokens. -
tokenizeToList
Splits a string into a list of tokens using specified separators, trimming whitespace and ignoring empty tokens. Uses comma as default separator if none provided. -
toBoolean
Converts string to Boolean based on common boolean representations. Supports values like 'true'/'false', 'yes'/'no', 'on'/'off', '1'/'0', etc. Returns null if the input cannot be parsed. -
toBoolean
-