Package com.xxl.tool.core
Class StringTool
java.lang.Object
com.xxl.tool.core.StringTool
string tool
- Author:
- xuxueli 2020-04-16 (some references to other libraries)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe empty String"".static final intRepresents a failed index search. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intcountMatches(String str, String sub) Count matchesstatic booleanstring equalsstatic Stringformat stringstatic StringformatWithMap(String template, Map<String, Object> params) format string with mapstatic booleanis blankstatic booleanis emptystatic booleanisNotBlank(String str) is not blankstatic booleanisNotEmpty(String str) is not emptystatic booleanisNumericstatic Stringjoin array to stringstatic Stringjoin array to stringstatic StringlowerCaseFirst(String text) lowerCase first letterstatic StringremovePrefix(String str, String prefix) remove prefixstatic StringremoveSuffix(String str, String suffix) remove suffixstatic Stringreplace stringsplit str 2 array, with separatorsplit str 2 array, with separatorstatic Stringsubstring from start positionstatic Stringsubstring from start position to end positionstatic Stringtrimstatic StringtrimToEmpty(String str) trimToEmptystatic StringtrimToNull(String str) trimToNullstatic StringunderlineToCamelCase(String underscoreText) convert underscores to hump formatstatic StringupperCaseFirst(String text) upperCase first letter
-
Field Details
-
EMPTY
The empty String"".- See Also:
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUNDRepresents a failed index search.- See Also:
-
-
Constructor Details
-
StringTool
public StringTool()
-
-
Method Details
-
isEmpty
is emptyStringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false- Parameters:
str-- Returns:
-
isNotEmpty
is not emptyStringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true- Parameters:
str-- Returns:
-
isBlank
is blankStringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false- Parameters:
str-- Returns:
-
isNotBlank
is not blankStringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true- Parameters:
str-- Returns:
-
trim
trimStringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"- Parameters:
str-- Returns:
-
trimToNull
trimToNullStringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"- Parameters:
str-- Returns:
-
trimToEmpty
trimToEmptyStringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"- Parameters:
str-- Returns:
-
isNumeric
isNumericChecks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
nullwill returnfalse. An empty String (length()=0) will returntrue.StringUtils.isNumeric(null) = false StringUtils.isNumeric("") = false StringUtils.isNumeric(" ") = false StringUtils.isNumeric("123") = true StringUtils.isNumeric("12 3") = false StringUtils.isNumeric("ab2c") = false StringUtils.isNumeric("12-3") = false StringUtils.isNumeric("12.3") = false- Parameters:
str- the String to check, may be null- Returns:
trueif only contains digits, and is non-null
-
countMatches
Count matchesCounts how many times the substring appears in the larger string.
A
nullor empty ("") String input returns0.StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", null) = 0 StringUtils.countMatches("abba", "") = 0 StringUtils.countMatches("abba", "a") = 2 StringUtils.countMatches("abba", "ab") = 1 StringUtils.countMatches("abba", "xxx") = 0- Parameters:
str- the CharSequence to check, may be nullsub- the substring to count, may be null- Returns:
- the number of occurrences, 0 if either CharSequence is
null
-
upperCaseFirst
upperCase first letterStringUtils.upperCaseFirst(null, *) = null StringUtils.upperCaseFirst("", *) = "" StringUtils.countMatches("abc", *) = "Abc"- Parameters:
text-- Returns:
-
lowerCaseFirst
lowerCase first letterStringUtils.lowerCaseFirst(null, *) = null StringUtils.lowerCaseFirst("", *) = "" StringUtils.lowerCaseFirst("ABC", *) = "aBC"- Parameters:
text-- Returns:
-
underlineToCamelCase
convert underscores to hump formatStringUtils.lowerCaseFirst(null, *) = null StringUtils.lowerCaseFirst("", *) = "" StringUtils.lowerCaseFirst("aaa_bbb", *) = "aaaBbb"- Parameters:
underscoreText-- Returns:
-
substring
substring from start positionStringTool.substring(null, *) = null StringTool.substring("", *) = "" StringTool.substring("abc", 0) = "abc" StringTool.substring("abc", 2) = "c" StringTool.substring("abc", 4) = "" StringTool.substring("abc", -2) = "abc"- Parameters:
str- the String to get the substring from, may be nullstart- the position to start from, negative means
-
substring
substring from start position to end positionStringTool.substring(null, *, *) = null StringTool.substring("", * , *) = ""; StringTool.substring("abc", 1, 2) = "b" StringTool.substring("abc", -1, 2) = "ab" StringTool.substring("abc", 1, 0) = "" StringTool.substring("abc", 1, 5) = "bc" StringTool.substring("abc", 2, 1) = ""- Parameters:
str- the String to get the substring from, may be nullstart- the position to start from, negative meansend- the position to end at (exclusive), negative means
-
split
split str 2 array, with separatorStringTool.split("a,b,c", ",") = ["a","b","c"] StringTool.split("a,b,", ",") = ["a","b"] StringTool.split("a, ,c", ",") = ["a","c"]- Parameters:
str- string to splitseparator- separator to use for separating elements- Returns:
-
split
public static List<String> split(String str, String separator, boolean trimTokens, boolean ignoreBlackTokens) split str 2 array, with separatorStringTool.split("a,b,c", ",") = ["a","b","c"] StringTool.split("a, b ,c", ",") = ["a","b","c"] StringTool.split("a,,c", ",") = ["a","c"]- Parameters:
str- string to splitseparator- separator to use for separating elementstrimTokens- true if the tokens should be trimmedignoreBlackTokens- true if empty tokens should be removed from the result- Returns:
-
join
join array to stringStringTool.join(["a","b","c"], ",") = "a,b,c" StringTool.join(["a","b"," c "], ",") = "a,b,c" StringTool.join(["a","b",""], ",") = "a,b" StringTool.join(["a",null,"c"], ",") = "a,c"- Parameters:
list- list to joinseparator- separator to use between elements- Returns:
-
join
public static String join(List<String> list, String separator, boolean trimTokens, boolean ignoreBlackTokens) join array to string- Parameters:
list- list to joinseparator- separator to use between elementstrimTokens- true if the tokens should be trimmedignoreBlackTokens- true if empty tokens should be ignored- Returns:
-
format
format stringStringTool.format("hello,{0}!", "world")); = hello,world! StringTool.format("hello,{0}!", null)); = hello,{0}! StringTool.format("hello,{0}!")); = hello,{0}! StringTool.format("hello,{0}!", "world", "world")); = hello,world! StringTool.format("Hello {0}, welcome {1}!")); = Hello {0}, welcome {1}! StringTool.format("Hello {0}, welcome {1}!",null)); = Hello {0}, welcome {1}! StringTool.format("Hello {0}, welcome {1}!",null, null)); = Hello null, welcome null! StringTool.format("Hello {0}, welcome {1}!", "Alice")); = Hello Alice, welcome {1}! StringTool.format("Hello {0}, welcome {1}!", "Alice", "Jack")); = Hello Alice, welcome Jack! StringTool.format("Hello {0}, welcome {1}!", "Alice", "Jack", "Lucy")); = Hello Alice, welcome Jack! StringTool.format("Hello {0}, you have {1} messages", "Alice", 5)); = Hello Alice, you have 5 messages StringTool.format("{1} messages for {0}", "Alice", 5)); = 5 messages for Alice StringTool.format("Hello {0}, welcome {0}!", "Alice")); = Hello Alice, welcome Alice! StringTool.format("Balance: {0,number}", 1234.56)); = Balance: 1,234.56 StringTool.format("Price: {0,number,currency}", 1234.56)); = Price: ¥1,234.56 StringTool.format("Success rate: {0,number,percent}", 0.85)); = Success rate: 85% StringTool.format("Account: {0,number,#,##0.00}", 1234.5)); = Account: 1,234.50- Parameters:
template- template stringparams- string array- Returns:
-
formatWithMap
format string with mapStringTool.formatWithMap("{name} is {age} years old", MapTool.newMap("name", "jack", "age", 18)) = jack is 18 years old StringTool.formatWithMap("{name} is {age} years old", null) = {name} is {age} years old StringTool.formatWithMap("{name} is {age} years old", MapTool.newMap("name", "jack")) = jack is {age} years old StringTool.formatWithMap("{name} is {age} years old", MapTool.newMap("name", "jack", "age", null)) = jack is {age} years old- Parameters:
template- template stringparams- parameter map- Returns:
-
replace
replace stringStringTool.replace("hello jack, how are you", "jack", "lucy")); = hello lucy, how are you StringTool.replace("hello jack, how are you, jack", "jack", "lucy")); = hello lucy, how are you, lucy StringTool.replace("", "jack", "lucy")); = StringTool.replace(null, "jack", "lucy")); = null StringTool.replace("hello jack, how are you", null, "jack")); = hello jack, how are you StringTool.replace("hello jack, how are you", "", "jack")); = hello jack, how are you StringTool.replace("hello jack, how are you", " ", "-")); = hello-jack,-how-are-you StringTool.replace("hello jack, how are you", "jack", null)); = hello jack, how are you StringTool.replace("hello jack, how are you", "jack", "")); = hello , how are you StringTool.replace("hello jack, how are you", "jack", " ")); = hello , how are you- Parameters:
inString- input stringoldPattern- old pattern, empty string will not be replacednewPattern- new pattern, null string will not to replace- Returns:
-
removePrefix
remove prefixStringTool.removePrefix("hello,world", "hello") = ,world StringTool.removePrefix("hello,world", "world") = hello,world StringTool.removePrefix("hello,world", "hello,world") = StringTool.removePrefix("hello,world", "") = hello,world StringTool.removePrefix("hello,world", null) = hello,world StringTool.removePrefix("", "world") = StringTool.removePrefix(null, "world") = null- Parameters:
str- the string to remove prefixprefix- prefix to remove
-
removeSuffix
remove suffixStringTool.removeSuffix("hello,world", "hello") hello,world StringTool.removeSuffix("hello,world", "world") hello, StringTool.removeSuffix("hello,world", "hello,world")) StringTool.removeSuffix("hello,world", "") hello,world StringTool.removeSuffix("hello,world", null) hello,world StringTool.removeSuffix("", "world") StringTool.removeSuffix(null, "world") null- Parameters:
str- the string to remove suffixsuffix- suffix to remove- Returns:
-
equals
string equalsStringTool.equals("hello", "hello") = true StringTool.equals("hello", "world") = false StringTool.equals(null, null) = true StringTool.equals(null, "world") = false StringTool.equals("hello", null) = false- Parameters:
str1- the first string to comparestr2- the second string to compare- Returns:
- true if equals
-