Package science.aist.jack.string
Class StringUtils
- java.lang.Object
-
- science.aist.jack.string.StringUtils
-
-
Field Summary
Fields Modifier and Type Field Description static int
INDEX_NOT_FOUND
Constant to define when an index is not foundstatic String
REPLACEMENT_PLACEHOLDER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
countMatches(String str, String sub)
Counts how often sub is included in strstatic <T> String
format(String s, Function<T,String> extractor, T... replacements)
ReplacesREPLACEMENT_PLACEHOLDER
with replacementsstatic <T> String
format(String s, T... replacements)
Calls StringUtilsformat(String, Function, Object[])
with functionObject.toString()
static List<Integer>
indicesOf(String string, String substring)
Returns the indices of every not overlapping occurrence of the substring in the given stringstatic List<Integer>
indicesOf(String string, String substring, boolean dontOverlap)
Returns the indices of every occurrence of the substring in the given stringstatic boolean
isNullOrBlank(String stringToCheck)
Returns iftrue
if the given stringToCheck is either null or blank, as inString.isBlank()
, otherwisefalse
static boolean
isNullOrEmpty(String stringToCheck)
Returns iftrue
if the given stringToCheck is either null or empty, as inString.isEmpty()
, otherwisefalse
static String
removeAll(@NonNull String stringToCheck, @NonNull Iterable<String> charsToRemove)
Removes all occurrences of a the given Strings in the stringToCheck.static String
replaceAll(@NonNull String stringToCheck, @NonNull Map<String,String> replacementMapper)
Replaces all occurrences of a the given Strings in the stringToCheck.
-
-
-
Field Detail
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUND
Constant to define when an index is not found- See Also:
- Constant Field Values
-
REPLACEMENT_PLACEHOLDER
public static final String REPLACEMENT_PLACEHOLDER
- See Also:
- Constant Field Values
-
-
Method Detail
-
format
@SafeVarargs public static <T> String format(String s, T... replacements)
Calls StringUtilsformat(String, Function, Object[])
with functionObject.toString()
- Type Parameters:
T
- the type of the replacements- Parameters:
s
- the string where theREPLACEMENT_PLACEHOLDER
should be replacedreplacements
- the replacement elements- Returns:
- the string with the replace elements
- See Also:
format(String, Function, Object[])
-
format
@SafeVarargs public static <T> String format(String s, Function<T,String> extractor, T... replacements)
ReplacesREPLACEMENT_PLACEHOLDER
with replacements- Type Parameters:
T
- the type of the replacements- Parameters:
s
- the string where theREPLACEMENT_PLACEHOLDER
should be replacedextractor
- the extractor function, that converts the replacement object into a stringreplacements
- the replacement elements- Returns:
- the string with the replace elements
- Throws:
IllegalArgumentException
- if the number of placeholders does not match the number of replacements
-
countMatches
public static int countMatches(String str, String sub)
Counts how often sub is included in str- Parameters:
str
- the string to be checksub
- the sub string to be search in str- Returns:
- the number of times sub occurs in str
-
indicesOf
public static List<Integer> indicesOf(String string, String substring)
Returns the indices of every not overlapping occurrence of the substring in the given string- Parameters:
string
- which contains the substringsubstring
- which is contained in the string- Returns:
- a list of all indices of every substring occurrence. List is empty if substring is not contained in string
-
indicesOf
public static List<Integer> indicesOf(String string, String substring, boolean dontOverlap)
Returns the indices of every occurrence of the substring in the given string- Parameters:
string
- which contains the substringsubstring
- which is contained in the stringdontOverlap
- flag which decides if the occurrence of a substring can be overlap with another occurrence (e.g. bb in bbb could result in the indices 0 and 1 or just in 0 if overlapping is not allowed)- Returns:
- a list of all indices of every substring occurrence. List is empty if substring is not contained in string
-
isNullOrEmpty
public static boolean isNullOrEmpty(String stringToCheck)
Returns iftrue
if the given stringToCheck is either null or empty, as inString.isEmpty()
, otherwisefalse
- Parameters:
stringToCheck
- the string to check for null or emptiness- Returns:
true
if the passed string is null or empty
-
isNullOrBlank
public static boolean isNullOrBlank(String stringToCheck)
Returns iftrue
if the given stringToCheck is either null or blank, as inString.isBlank()
, otherwisefalse
- Parameters:
stringToCheck
- the string to check for null or blankness- Returns:
true
if the passed string is null or blank, meaning it only contains whitespaces
-
removeAll
public static String removeAll(@NonNull @NonNull String stringToCheck, @NonNull @NonNull Iterable<String> charsToRemove)
Removes all occurrences of a the given Strings in the stringToCheck. the charsToRemove are iterated over and each element is used as a parameter in theString::replaceAll
method- Parameters:
stringToCheck
- the string, which contains characters that should be removed. If null, null is returnedcharsToRemove
- a collection of string containing strings that need to be replaced in the stringToCheck parameter- Returns:
- stringToCheck, where all charsToRemove have been replaced with the empty String
-
replaceAll
public static String replaceAll(@NonNull @NonNull String stringToCheck, @NonNull @NonNull Map<String,String> replacementMapper)
Replaces all occurrences of a the given Strings in the stringToCheck. the charsToRemove are iterated over and each key of the map is used as a the search parameter and each value as the replacement in theString::replace
method.- Parameters:
stringToCheck
- the string, which contains characters that should be removed. If null, null is returnedreplacementMapper
- key value pairs describing how one string should be replaced. Key is the search term, value is the replacement string- Returns:
- stringToCheck, where all charsToRemove have been replaced with the empty String
-
-