java.lang.Object
com.github.hypfvieh.util.StringUtil
Utility class for String manipulation.
- Seit:
- v1.0 - 2016-06-29
- Autor:
- hypfvieh
-
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic 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 StringRemoves trailing and leading spaces / non-printable characters (char <= 32).static StringupperCaseFirstChar(String _str) Upper case the first letter of the given string.
-
Methodendetails
-
abbreviate
Abbreviates a String using ellipses.- Parameter:
_str- string to abbrivate_length- max length- Gibt zurück:
- abbreviated string, original string if string length is lower or equal then desired length or null if input was null
-
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.- Parameter:
_text- text to split_len- max length per line- Gibt zurück:
- list or null if _text was null
-
splitEqually
Splits a Text to equal parts. There is no detection of words, everything will be cut to the same length.- Parameter:
_text- text to split_len- max length per line- Gibt zurück:
- list of string splitted to _len or null if _text was null
-
replaceByMap
Replace all placeholders in given string by value of the corresponding key in given Map.- Parameter:
_searchStr- search string_replacements- replacement- Gibt zurück:
- String or null if _searchStr was null
-
lowerCaseFirstChar
Lower case the first letter of the given string.- Parameter:
_str- string- Gibt zurück:
- lowercased string
-
upperCaseFirstChar
Upper case the first letter of the given string.- Parameter:
_str- string- Gibt zurück:
- uppercased string
-
rot13
Simple rot13 implementation.- Parameter:
_input- input to scramble- Gibt zurück:
- scrambled input (null if input was null)
-
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.- Parameter:
_str- string to check_compare- compare strings- Gibt zurück:
- 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.- Parameter:
_str- string to check_compare- compare strings- Gibt zurück:
- 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.- Parameter:
_ignoreCase- ignore case_str- string to check_compare- compare strings- Gibt zurück:
- true if equal false otherwise
-
isBlank
Checks if the given String is either null or blank. Blank means:
" " - true "" - true null - true " xx" - false
- Parameter:
_str- string to test- Gibt zurück:
- true if string is blank or null, false otherwise
-
isNotBlank
Checks if the given String is NOT blank.- Parameter:
_str- string to check- Gibt zurück:
- 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
- Parameter:
_str- string to test- Gibt zurück:
- 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.- Parameter:
_str- string to check_default- default in case of blank string- Gibt zurück:
- _str or _default
-
randomString
Generate a simple (cryptographic insecure) random string.- Parameter:
_length- length of random string- Gibt zurück:
- random string or empty string if _length <= 0
-
trim
Removes trailing and leading spaces / non-printable characters (char <= 32).- Parameter:
_str- String to trim- Gibt zurück:
- trimmed string or
nullif null input
-
join
Combines the Strings in _string using _delimiter.- Parameter:
_delimiter- delimiting string_strings- strings to join- Gibt zurück:
- null if _strings is null, concatenated string otherwise
-
join
Combines the Strings in _string using _delimiter.- Parameter:
_delimiter- delimiting string_strings- string to join- Gibt zurück:
- null if _strings is null, concatenated string otherwise
-
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"- Parameter:
_str- camel-case string- Gibt zurück:
- 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.- Parameter:
_str- string to convert- Gibt zurück:
- converted string or original string if there was nothing to do
-
isAllUpperCase
Checks if the given String is in all upper-case.- Parameter:
_str- string to check- Gibt zurück:
- 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.- Parameter:
_str- string to check_args- patterns to find- Gibt zurück:
- true if any string in _args is found in _str, false if not or _str/_args is null
-
containsAny
Checks if any of the given strings in _args is contained in _str, case-sensitive.- Parameter:
_str- string to check_args- patterns to find- Gibt zurück:
- true if any string in _args is found in _str, false if not or _str/_args is null
-
containsAny
Checks if any of the given strings in _args is contained in _str.- Parameter:
_ignoreCase- true to ignore case, false to be case sensitive_str- string to check_args- patterns to find- Gibt zurück:
- 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.- Parameter:
_ignoreCase- true to ignore case, false to be case sensitive_str- string to check_args- patterns to find- Gibt zurück:
- 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.- Parameter:
_ignoreCase- true to ignore case, false to be case sensitive_str- string to check_args- patterns to find- Gibt zurück:
- true if given string in _str starts with any of the given strings in _args, false if not or _str/_args is null
-
repeat
Repeats the given string pattern for the given times.- Parameter:
_str- string to repeat_count- number of repetitions- Gibt zurück:
- repeated string or null if pattern was null or count was <= 0
-
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"- Parameter:
_str- the String to pad out, may be null_size- the size to pad to_padChar- the padding character- Gibt zurück:
- right-padded String or original String if no padding is necessary
-
split
Split a String by the given delimiting char.- Parameter:
_str- string to split_separatorChar- delimiting char to use- Gibt zurück:
- null if _str is null, array (maybe empty) otherwise
-
split
Split a String by the given delimiting char.- Parameter:
_str- string to split_separatorStr- delimiting String to use- Gibt zurück:
- null if _str is null, array (maybe empty) otherwise
-
split
Split string by whitespace.- Parameter:
_str- string to split- Gibt zurück:
- null if _str is null, array (maybe empty) otherwise
-
splitToList
Split given String using given delimiting char.- Parameter:
_str- string to split_separatorChar- char to use as delimiter- Gibt zurück:
- list of String tokens, null if _str is null or empty list
-
splitToList
Split given String using the given delimiting string.- Parameter:
_str- string to split_separatorStr- string to us as delimiter- Gibt zurück:
- list of String tokens, null if _str is null or empty list
-
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.- Parameter:
_str- string to mask_maskStr- mask to use_maskBgn- offset to start at (0 based, inclusive)_maskRpt- repetitions of _maskStr- Gibt zurück:
- masked String or null
-
snakeToCamelCase
Converts a snake-case-string to camel case string.
Eg. this_is_snake_case → thisIsSnakeCase- Parameter:
_input- string- Gibt zurück:
- camel case string or input if nothing todo. Returns null if input was null.
-
concatStrings
Concats all strings using the separator as delimiter. Will exclude all null values and optionally ignore empty values.- Parameter:
_ignoreEmpty- true to ignore empty strings_separator- separator to add between each string_parts- parts to concat- Gibt zurück:
- 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.- Parameter:
_str- the sequence to check for blankness_message- exception message if check fails- Gibt zurück:
_strif notblank- Löst aus:
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.- Parameter:
_str- the sequence to check for emptiness_message- exception message if check fails- Gibt zurück:
_strif notempty- Löst aus:
IllegalArgumentException- if_strisempty
-
requireNonEmpty
-
countSubString
Count the number of instances of substring within a string.- Parameter:
string- String to look for substring in.substring- Sub-string to look for.- Gibt zurück:
- Count of substrings in string.
-