Class StringUtil
java.lang.Object
com.github.hypfvieh.util.StringUtil
Utility class for String manipulation.
- Since:
- v1.0 - 2016-06-29
- Author:
- hypfvieh
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringabbreviate(String _str, int _length) Abbreviates a String using ellipses.static StringconcatStrings(boolean _ignoreEmpty, String _separator, String... _parts) Concats all strings using the separator as delimiter.static booleancontainsAny(boolean _ignoreCase, String _str, String... _args) Checks if any of the given strings in _args is contained in _str.static booleancontainsAny(String _str, String... _args) Checks if any of the given strings in _args is contained in _str, case-sensitive.static booleancontainsAnyIgnoreCase(String _str, String... _args) Checks if any of the given strings in _args is contained in _str, case-insensitve.static StringConverts a camel-case string to an upper-case string where each upper-case character except the first in the input string is preceded by an underscore in the output string.static StringTries to convert upper-case string to camel-case.static intcountSubString(String string, String substring) Count the number of instances of substring within a string.static StringdefaultIfBlank(String _str, String _default) Checks if given String is blank (seeisBlank(String).
If String is blank, the given default is returned, otherwise the String is returned.static booleanendsWithAny(boolean _ignoreCase, String _str, String... _args) Checks if given string in _str ends with any of the given strings in _args.static booleanChecks if any of the given strings in _compare is equal to _str (either case-insensitive or case-sensitive).
Will return true if both parameters are null or _str is null and _compare is empty.static booleanChecks if any of the given strings in _compare is equal to _str (case-sensitive).
Will return true if both parameters are null or _str is null and _compare is empty.static booleanequalsIgnoreCaseAny(String _str, String... _compare) Checks if any of the given strings in _compare is equal to _str (case-insensitive).
Will return true if both parameters are null or _str is null and _compare is empty.static booleanisAllUpperCase(String _str) Checks if the given String is in all upper-case.static booleanisBlank(CharSequence _str) Checks if the given String is either null or blank.static booleanisEmpty(CharSequence _str) Checks if the given String is either null or empty.static booleanisNotBlank(CharSequence _str) Checks if the given String is NOT blank.static StringCombines the Strings in _string using _delimiter.static StringCombines the Strings in _string using _delimiter.static StringlowerCaseFirstChar(String _str) Lower case the first letter of the given string.static StringMask the given string with the given pattern starting at given start and ending on given end of string.static StringrandomString(int _length) Generate a simple (cryptographic insecure) random string.static StringRepeats the given string pattern for the given times.static StringreplaceByMap(String _searchStr, Map<String, String> _replacements) Replace all placeholders in given string by value of the corresponding key in given Map.static <T extends CharSequence>
TrequireNonBlank(T _str) static <T extends CharSequence>
TrequireNonBlank(T _str, String _message) Checks that the specified string is notblank.static <T extends CharSequence>
TrequireNonEmpty(T _str) static <T extends CharSequence>
TrequireNonEmpty(T _str, String _message) Checks that the specified string is notempty.static StringRight-pads a string with spaces (' ').
The String is padded to the size of_size.static StringSimple rot13 implementation.smartWordSplit(String _text, int _len) Tries to split a string in a smart way.
String will be splitted by space and then recombined until each line has the given length or less than the given length, if the next token would cause the line to be longer than requested It is ensured that each line has a maximum length of _len, it could be short but never longer.static StringsnakeToCamelCase(String _input) Converts a snake-case-string to camel case string.static String[]Split string by whitespace.static String[]Split a String by the given delimiting char.static String[]Split a String by the given delimiting char.splitEqually(String _text, int _len) Splits a Text to equal parts.splitToList(String _str, char _separatorChar) Split given String using given delimiting char.splitToList(String _str, String _separatorStr) Split given String using the given delimiting string.static booleanstartsWithAny(boolean _ignoreCase, String _str, String... _args) Checks if given string in _str starts with any of the given strings in _args.static StringsubstringAfterLast(String _str, String _separator) static StringRemoves trailing and leading spaces / non-printable characters (char <= 32).static StringtrimToEmpty(String _str) Removes trailing and leading spaces / non-printable characters (char <= 32) and returns an empty String if remaining string would be null.static StringtrimToNull(String _str) Removes trailing and leading spaces / non-printable characters (char <= 32) and returns null if remaining string would be empty.static StringupperCaseFirstChar(String _str) Upper case the first letter of the given string.
-
Method Details
-
abbreviate
-
smartWordSplit
Tries to split a string in a smart way.
String will be splitted by space and then recombined until each line has the given length or less than the given length, if the next token would cause the line to be longer than requested It is ensured that each line has a maximum length of _len, it could be short but never longer.- Parameters:
_text- text to split_len- max length per line- Returns:
- list or null if _text was null
-
splitEqually
-
replaceByMap
-
lowerCaseFirstChar
-
upperCaseFirstChar
-
rot13
-
equalsIgnoreCaseAny
Checks if any of the given strings in _compare is equal to _str (case-insensitive).
Will return true if both parameters are null or _str is null and _compare is empty.- Parameters:
_str- string to check_compare- compare strings- Returns:
- true if equal false otherwise
-
equalsAny
Checks if any of the given strings in _compare is equal to _str (case-sensitive).
Will return true if both parameters are null or _str is null and _compare is empty.- Parameters:
_str- string to check_compare- compare strings- Returns:
- true if equal false otherwise
-
equalsAny
Checks if any of the given strings in _compare is equal to _str (either case-insensitive or case-sensitive).
Will return true if both parameters are null or _str is null and _compare is empty.- Parameters:
_ignoreCase- ignore case_str- string to check_compare- compare strings- Returns:
- true if equal false otherwise
-
isBlank
Checks if the given String is either null or blank. Blank means:
" " - true "" - true null - true " xx" - false
- Parameters:
_str- string to test- Returns:
- true if string is blank or null, false otherwise
-
isNotBlank
Checks if the given String is NOT blank.- Parameters:
_str- string to check- Returns:
- true if string is not blank, false otherwise
-
isEmpty
Checks if the given String is either null or empty. Blank means:
" " - false "" - true null - true " xx" - false
- Parameters:
_str- string to test- Returns:
- true if string is empty or null, false otherwise
-
defaultIfBlank
Checks if given String is blank (seeisBlank(String).
If String is blank, the given default is returned, otherwise the String is returned.- Parameters:
_str- string to check_default- default in case of blank string- Returns:
- _str or _default
-
randomString
Generate a simple (cryptographic insecure) random string.- Parameters:
_length- length of random string- Returns:
- random string or empty string if _length <= 0
-
trim
-
trimToNull
-
trimToEmpty
-
join
-
join
-
convertCamelToUpperCase
Converts a camel-case string to an upper-case string where each upper-case character except the first in the input string is preceded by an underscore in the output string. Empty or null strings are returned as-is.convertCamelToUpperCase(null) = null convertCamelToUpperCase("") = "" convertCamelToUpperCase(" ") = " " convertCamelToUpperCase("Hello") = "HELLO" convertCamelToUpperCase("HELLO") = "HELLO" convertCamelToUpperCase("AcmeCompany") = "ACME_COMPANY"- Parameters:
_str- camel-case string- Returns:
- upper-case string
-
convertUpperToCamelCase
Tries to convert upper-case string to camel-case. The given string will be analyzed and all string parts preceded by an underline character will be converted to upper-case, all other following characters to lower-case.- Parameters:
_str- string to convert- Returns:
- converted string or original string if there was nothing to do
-
isAllUpperCase
Checks if the given String is in all upper-case.- Parameters:
_str- string to check- Returns:
- true if upper-case, false otherwise. Also false if string is null or empty.
-
containsAnyIgnoreCase
Checks if any of the given strings in _args is contained in _str, case-insensitve.- Parameters:
_str- string to check_args- patterns to find- Returns:
- true if any string in _args is found in _str, false if not or _str/_args is null
-
containsAny
-
containsAny
Checks if any of the given strings in _args is contained in _str.- Parameters:
_ignoreCase- true to ignore case, false to be case sensitive_str- string to check_args- patterns to find- Returns:
- true if any string in _args is found in _str, false if not or _str/_args is null
-
endsWithAny
Checks if given string in _str ends with any of the given strings in _args.- Parameters:
_ignoreCase- true to ignore case, false to be case sensitive_str- string to check_args- patterns to find- Returns:
- true if given string in _str ends with any of the given strings in _args, false if not or _str/_args is null
-
startsWithAny
Checks if given string in _str starts with any of the given strings in _args.- Parameters:
_ignoreCase- true to ignore case, false to be case sensitive_str- string to check_args- patterns to find- Returns:
- true if given string in _str starts with any of the given strings in _args, false if not or _str/_args is null
-
repeat
-
rightPad
Right-pads a string with spaces (' ').
The String is padded to the size of_size.StringUtil.rightPad(null, *) = null StringUtil.rightPad("", 3) = " " StringUtil.rightPad("bat", 3) = "bat" StringUtil.rightPad("bat", 5) = "bat " StringUtil.rightPad("bat", 1) = "bat" StringUtil.rightPad("bat", -1) = "bat"- Parameters:
_str- the String to pad out, may be null_size- the size to pad to_padChar- the padding character- Returns:
- right-padded String or original String if no padding is necessary
-
split
-
split
-
split
-
splitToList
-
splitToList
-
mask
Mask the given string with the given pattern starting at given start and ending on given end of string.
If _str is null or _maskStr is null or empty, null is returned.
If _maskBgn is lower than 0 or _maskLength is lower than 0 or _maskRpt minus _maskBgn is lower than 0, null is returned.
If _maskBgn is bigger than the length of _str, the original String is returned.
If _maskRpt is bigger than the length of _str, length of _str is used.- Parameters:
_str- string to mask_maskStr- mask to use_maskBgn- offset to start at (0 based, inclusive)_maskRpt- repetitions of _maskStr- Returns:
- masked String or null
-
snakeToCamelCase
-
concatStrings
Concats all strings using the separator as delimiter. Will exclude all null values and optionally ignore empty values.- Parameters:
_ignoreEmpty- true to ignore empty strings_separator- separator to add between each string_parts- parts to concat- Returns:
- concatinated string, null if input is null
-
requireNonBlank
Checks that the specified string is notblank. This method is designed primarily for doing parameter validation in methods and constructors.- Parameters:
_str- the sequence to check for blankness_message- exception message if check fails- Returns:
_strif notblank- Throws:
IllegalArgumentException- if_strisblank
-
requireNonBlank
-
requireNonEmpty
Checks that the specified string is notempty. This method is designed primarily for doing parameter validation in methods and constructors.- Parameters:
_str- the sequence to check for emptiness_message- exception message if check fails- Returns:
_strif notempty- Throws:
IllegalArgumentException- if_strisempty
-
requireNonEmpty
-
countSubString
-
substringAfterLast
-