Class StringUtils
- Author:
- Yury Molchan
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcamelToKebab(String camel) Converts CamelCased string to kebab-cased.static Stringcapitalize(String str) Capitalizes a String changing the first letter to title case as perCharacter.toTitleCase(char).static booleancontainsIgnoreCase(String str, String searchStr) Checks if String contains a search String irrespective of case, handlingnull.static intfirst(CharSequence text, int start, int end, IntPredicate tester) Find position of the first occurrence of the symbol is matched to the predicate.static intfirstNonSpace(CharSequence text, int start, int end) Find position of the next not white space symbol.static booleanisBlank(CharSequence cs) Checks if a CharSequence is whitespace, empty ("") or null.static booleanisEmpty(CharSequence cs) Checks if a CharSequence is empty ("") or null.static booleanChecks if a CharSequence is not empty (""), not null and not whitespace only.static booleanChecks if a CharSequence is not empty ("") and not null.static booleanisSpaceOrControl(int ch) Determines if the specified character is whitespace or control character (char <= 32 or (>= 127 and <= 160)static StringJoins the elements of the provided array into a single String containing the provided list of elements.static intlast(CharSequence text, int start, int end, IntPredicate tester) Find position of the last occurrence of the symbol is matched to the predicate.static intlastNonSpace(CharSequence text, int start, int end) Find position of the last not white space symbol.static booleanmatches(Pattern regex, CharSequence input) Do the same asString.matches(String)static String[]Splits the provided text into an array, using whitespace as the separator.static String[]Splits the provided string into an array of trimmed strings, separator specified.static String[]Splits the provided string into an array of trimmed strings, separated by new line character codes:'\n'and'\r'.static StringRemoves control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String, handlingnullby returningnull.static StringtrimToEmpty(String str) Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it isnull.static StringtrimToNull(String str) Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String returningnullif the String is empty ("") after the trim or if it isnull.static Stringuncapitalize(String str) Uncapitalizes a String changing the first letter to title case as perCharacter.toLowerCase(char).
-
Field Details
-
EMPTY_STRING_ARRAY
-
EMPTY
- See Also:
-
SPACE
- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
split
Splits the provided string into an array of trimmed strings, separator specified. The separator is not included in the returned String array. Adjacent separators are treated as one separator.
A
nullinput String returnsnull.StringUtils.split(null, *) = null StringUtils.split("", *) = [] StringUtils.split("a.b.c", '.') = ["a", "b", "c"] StringUtils.split("a..b.c", '.') = ["a", "b", "c"] StringUtils.split("a . .b.c", '.') = ["a", "b", "c"] StringUtils.split(" a b:c ", '.') = ["a b:c"] StringUtils.split("a b c", ' ') = ["a", "b", "c"] StringUtils.split(" a, b, c", ',') = ["a", "b", "c"]- Parameters:
str- the String to parse, may be nullseparator- the character used as the delimiter- Returns:
- an array of parsed Strings,
nullif null String input
-
split
Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by
Character.isWhitespace(char).The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A
nullinput String returnsnull.StringUtils.split(null) = null StringUtils.split("") = [] StringUtils.split("abc def") = ["abc", "def"] StringUtils.split("abc def") = ["abc", "def"] StringUtils.split(" abc ") = ["abc"] StringUtils.split(" abc def ") = ["abc", "def"]- Parameters:
str- the String to parse, may be null- Returns:
- an array of parsed Strings,
nullif null String input
-
toLines
Splits the provided string into an array of trimmed strings, separated by new line character codes:
'\n'and'\r'. Blank lines are omitted from the result.A blank or
nullinput String returnsnull.StringUtils.toLines(null) = null StringUtils.toLines("") = null StringUtils.toLines(" ") = null StringUtils.toLines(" abc ") = ["abc"] StringUtils.toLines("a b c") = ["a b c"] StringUtils.toLines("a\rb\nc") = ["a", "b", "c"] StringUtils.toLines("\r a\n\t\r \n c \n") = ["a", "c"]- Parameters:
text- the String to parse, may be null- Returns:
- an array of parsed Strings,
nullif blank String 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.StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "null" StringUtils.join(["a", "b", "c"], "--") = "a--b--c" StringUtils.join(["a", "b", "c"], "") = "abc" StringUtils.join([null, "", "a"], ',') = "null,,a"
- Parameters:
values- the array of values to join together, may be nullseparator- the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null array input
-
isEmpty
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("boo") = false StringUtils.isEmpty(" boo ") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null
-
isNotEmpty
Checks if a CharSequence is not empty ("") and not null.
StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("boo") = true StringUtils.isNotEmpty(" boo ") = true- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is not empty and not null
-
isBlank
Checks if a CharSequence is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("boo") = false StringUtils.isBlank(" boo ") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is null, empty or whitespace
-
isNotBlank
Checks if a CharSequence is not empty (""), not null and not whitespace only.
StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("boo") = true StringUtils.isNotBlank(" boo ") = true- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is not empty and not null and not whitespace
-
containsIgnoreCase
Checks if String contains a search String irrespective of case, handling
null. Case-insensitivity is defined as byString.equalsIgnoreCase(String).A
nullString will returnfalse.StringUtils.contains(null, *) = false StringUtils.contains(*, null) = false StringUtils.contains("", "") = true StringUtils.contains("abc", "") = true StringUtils.contains("abc", "a") = true StringUtils.contains("abc", "Bc") = true StringUtils.contains("abc", "z") = false StringUtils.contains("abc", "Z") = false- Parameters:
str- the String to check, may be nullsearchStr- the String to find, may be null- Returns:
- true if the String contains the search String irrespective of case or false if not or
nullstring input
-
matches
Do the same asString.matches(String)- Parameters:
regex- a Pattern to which this string is to be matchedinput- an input string to match regexp Pattern- Returns:
trueif, and only if, this string matches the given regular expression
-
trim
Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String, handling
nullby returningnull.StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("boo") = "boo" StringUtils.trim(" boo ") = "boo"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed string,
nullif null String input
-
isSpaceOrControl
public static boolean isSpaceOrControl(int ch) Determines if the specified character is whitespace or control character (char <= 32 or (>= 127 and <= 160)- Parameters:
ch- the character to be tested- Returns:
trueif character is whitespace or control, otherwisefalse
-
trimToNull
Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String returning
nullif the String is empty ("") after the trim or if it isnull.StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("boo") = "boo" StringUtils.trimToNull(" boo ") = "boo"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed String,
nullif only chars <= 32, empty or null String input
-
trimToEmpty
Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is
null.StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("boo") = "boo" StringUtils.trimToEmpty(" boo ") = "boo"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed String, or an empty String if
nullinput
-
capitalize
Capitalizes a String changing the first letter to title case as per
Character.toTitleCase(char). No other letters are changed.A
nullinput String returnsnull.StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("foo") = "Foo" StringUtils.capitalize("fOo") = "FOo"- Parameters:
str- the String to capitalize, may be null- Returns:
- the capitalized String,
nullif null String input - See Also:
-
uncapitalize
Uncapitalizes a String changing the first letter to title case as per
Character.toLowerCase(char). No other letters are changed.A
nullinput String returnsnull.StringUtils.uncapitalize(null) = null StringUtils.uncapitalize("") = "" StringUtils.uncapitalize("Foo") = "foo" StringUtils.uncapitalize("FOO") = "fOO"- Parameters:
str- the String to uncapitalize, may be null- Returns:
- the uncapitalized String,
nullif null String input - See Also:
-
camelToKebab
Converts CamelCased string to kebab-cased. It is useful for converting Java's fields or methods to case insensetive form, e.g. for properties keys, urls, MS Windows file names and e.t.c.StringUtils.camelToKebab(null) = null StringUtils.camelToKebab("") = "" StringUtils.camelToKebab("FOO") = "foo" StringUtils.camelToKebab("Foo") = "foo" StringUtils.camelToKebab("foo") = "foo" StringUtils.camelToKebab("FooBar") = "foo-bar" StringUtils.camelToKebab("fooBar") = "foo-bar" StringUtils.camelToKebab("FOOBar") = "foo-bar" StringUtils.camelToKebab("ABar") = "a-bar" StringUtils.camelToKebab("aBar") = "a-bar" StringUtils.camelToKebab("aBAR") = "a-bar"- Parameters:
camel- - CamelCased string- Returns:
- - kebab-cased string
-
first
Find position of the first occurrence of the symbol is matched to the predicate.- Parameters:
text- the string where a not white space symbol should be searchedstart- the start position of the searching inclusiveend- the final position of searching exclusivetester- the predicate for searching the symbol- Returns:
- position of the symbol or -1
-
last
Find position of the last occurrence of the symbol is matched to the predicate.- Parameters:
text- the string where a not white space symbol should be searchedstart- the start position of the searching inclusiveend- the final position of searching exclusivetester- the predicate for searching the symbol- Returns:
- position of the symbol or -1
-
firstNonSpace
Find position of the next not white space symbol.- Parameters:
text- the string where a not white space symbol should be searchedstart- the start position of the searching inclusiveend- the final position of searching exclusive- Returns:
- position of a not white space symbol or -1
-
lastNonSpace
Find position of the last not white space symbol.- Parameters:
text- the string where a not white space symbol should be searchedstart- the start position of the searching inclusiveend- the final position of searching exclusive- Returns:
- position of a not white space symbol or -1
-