Class StringUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcapitalizeFirstLetter(String string) Capitalizes the first letter.static intcountPatternOccurrences(Pattern pattern, String statement) Obtains the numbers of question marks in a String.static StringformatCollection(Collection<?> collection) Formats a collection of objects into a string.static StringformatCollection(Collection<?> collection, int indent) Formats a collection of objects into a string.static <T> StringformatCollection(Collection<T> collection, Function<T, String> mapper) Formats a collection of objects into a string.static <T> StringformatCollection(Collection<T> collection, Function<T, String> mapper, int indent) Formats a collection of objects into a string.static <T> StringformatCollection(Collection<T> collection, Function<T, String> mapper, int indent, boolean printSize) Formats a collection of objects into a string.static it.unimi.dsi.fastutil.ints.IntImmutableListgetVariableIndices(String string, char variable) Gets all indices of a given character in a string.static booleanisValidStructureName(@Nullable String name) Check if a given string is a valid structure name.static StringrandomString(int length) Generate an (insecure) random alphanumeric string of a given length.static StringremoveTrailingNewlines(CharSequence string) Removes all trailing newlines from aString.static StringBuilderRemoves all trailing newlines from aStringBuilder.Creates a collector that collects a stream of strings into a formatted string.stringCollector(String delimiter) Creates a collector that collects a stream of strings into a formatted string.stringCollector(String delimiter, String emptyString) Creates a collector that collects a stream of strings into a formatted string.static StringtoString(ExecutorService executor) Converts anExecutorServiceto a string.
-
Method Details
-
toString
Converts anExecutorServiceto a string.This method is used for logging/debugging purposes.
- Parameters:
executor- TheExecutorServiceto convert.- Returns:
- A string representation of the
ExecutorService.
-
randomString
Generate an (insecure) random alphanumeric string of a given length.- Parameters:
length- Length of the resulting string- Returns:
- An insecure random alphanumeric string.
-
removeTrailingNewlines
Removes all trailing newlines from aStringBuilder.- Parameters:
sb- TheStringBuilderto remove the trailing newlines from.This object is modified in place.
If the
StringBuilderis empty or does not end with a newline, nothing is done.- Returns:
- The
StringBuilderfor chaining.
-
removeTrailingNewlines
Removes all trailing newlines from aString. -
capitalizeFirstLetter
-
countPatternOccurrences
-
isValidStructureName
Check if a given string is a valid structure name. The following input is not allowed:- Numerical names (numerical values are reserved for UIDs).
- Empty input.
- Input containing spaces.
- Parameters:
name- The name to test for validity,- Returns:
- True if the name is allowed.
-
formatCollection
public static <T> String formatCollection(Collection<T> collection, Function<T, String> mapper, int indent, boolean printSize) Formats a collection of objects into a string.The objects are formatted using the provided mapper function. The resulting strings are joined with a delimiter and indented by the provided amount.
For example, given a collection of Strings ["A", "B", "C"], a mapper that returns the input string, and an indent of 0, the resulting string would be:
- A - B - CNote that there is no trailing delimiter or new line.
- Type Parameters:
T- The type of the objects in the collection.- Parameters:
collection- The collection of objects to format.mapper- The function to map the objects to strings.indent- The amount of spaces to indent the lines. This is the number of spaces before each '-'.printSize- Whether to print the size of the collection in parentheses before the formatted strings.- Returns:
- A formatted string.
-
formatCollection
public static <T> String formatCollection(Collection<T> collection, Function<T, String> mapper, int indent) Formats a collection of objects into a string.This method is a shortcut for
formatCollection(Collection, Function, int, boolean)with an indent of 0 and printSize enabled.- Type Parameters:
T- The type of the objects in the collection.- Parameters:
collection- The collection of objects to format.mapper- The function to map the objects to strings.- Returns:
- A formatted string.
-
formatCollection
Formats a collection of objects into a string.This method is a shortcut for
formatCollection(Collection, Function, int, boolean)with an indent of 0 and printSize enabled.- Type Parameters:
T- The type of the objects in the collection.- Parameters:
collection- The collection of objects to format.mapper- The function to map the objects to strings.- Returns:
- A formatted string.
-
formatCollection
Formats a collection of objects into a string.This method is a shortcut for
formatCollection(Collection, Function, int, boolean)with a mapper that callsObject.toString()and printSize enabled.- Parameters:
collection- The collection of objects to format.- Returns:
- A formatted string.
-
formatCollection
Formats a collection of objects into a string.This method is a shortcut for
formatCollection(Collection, Function, int, boolean)with an indent of 0 and the mapper that callsObject.toString()and printSize enabled.- Parameters:
collection- The collection of objects to format.- Returns:
- A formatted string.
-
stringCollector
Creates a collector that collects a stream of strings into a formatted string.For example, given a delimiter of "\n - " and an empty String of "{}", and a stream with values "1", "2", and "3", the resulting string would be:
- 1 - 2 - 3Given the same delimiter and empty string, but an empty stream, the resulting string would be:
{}Note that there is no trailing delimiter or new line.
Example usage:
List<String> list = List.of("1", "2", "3"); String formatted = list.stream().collect(StringUtil.stringCollector("\n - ", "{}"));- Parameters:
delimiter- The delimiter to prepend to each string in the stream.emptyString- The string representation when the stream is empty.- Returns:
- A collector that collects the stream into a formatted string.
-
stringCollector
Creates a collector that collects a stream of strings into a formatted string.This method is a shortcut for
stringCollector(String, String)with the empty string "{}".- Parameters:
delimiter- The delimiter to prepend to each string in the stream.- Returns:
- A collector that collects the stream into a formatted string.
-
stringCollector
Creates a collector that collects a stream of strings into a formatted string.This method is a shortcut for
stringCollector(String, String)with the empty string "{}" and the delimiter "\n- ".- Returns:
- A collector that collects the stream into a formatted string.
-
getVariableIndices
public static it.unimi.dsi.fastutil.ints.IntImmutableList getVariableIndices(String string, char variable) Gets all indices of a given character in a string.For example, given the string "Hello, World!" and the character 'o', this method would return the list [4, 8].
- Parameters:
string- The string to search in.- Returns:
- A list of all indices of the given character in the string.
-