@ThreadSafe public final class Strings extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
Strings.CharacterPredicate
Represents a predicate (boolean-valued function) of one character argument.
|
static class |
Strings.Justify |
private static class |
Strings.RegExSplitter
A tokenization class used to split a comma-separated list of regular expressions.
|
| Modifier and Type | Field and Description |
|---|---|
private static String |
CURLY_PREFIX |
private static String |
CURLY_SUFFIX |
private static String |
DEFAULT_DELIM |
private static Pattern |
TIME_PATTERN |
private static String |
VAR_DELIM |
| Modifier | Constructor and Description |
|---|---|
private |
Strings() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
asBoolean(String value,
boolean defaultValue)
Parse the supplied string as a boolean value.
|
static double |
asDouble(String value,
double defaultValue)
Parse the supplied string as a double value.
|
static Duration |
asDuration(String timeString)
Converts the given string (in the format 00:00:00(.0*)) into a
Duration. |
static int |
asInt(String value,
int defaultValue)
Parse the supplied string as a integer value.
|
static long |
asLong(String value,
long defaultValue)
Parse the supplied string as a long value.
|
static Number |
asNumber(String value)
Parse the supplied string as a number.
|
static Number |
asNumber(String value,
Supplier<Number> defaultValueProvider)
Parse the supplied string as a number.
|
static int |
compareTo(CharSequence str1,
CharSequence str2)
Compare two
CharSequence instances. |
static String |
createString(char charToRepeat,
int numberOfRepeats)
Create a new string containing the specified character repeated a specific number of times.
|
private static Character |
deriveQuotingChar(String identifierPart) |
static String |
duration(long durationInMillis)
For the given duration in milliseconds, obtain a readable representation of the form
HHH:MM:SS.mmm, where
HHH
is the number of hours written in at least 2 digits (e.g., "03")
MM
is the number of hours written in at least 2 digits (e.g., "05")
SS
is the number of hours written in at least 2 digits (e.g., "09")
mmm
is the fractional part of seconds, written with 1-3 digits (any trailing zeros are dropped)
|
static boolean |
equalsIgnoreCase(String str1,
String str2)
Check whether the two
String instances are equal ignoring case. |
static String |
getBegin(String str,
int length)
Returns the first
length characters of the given string. |
static String |
getStackTrace(Throwable throwable)
Get the stack trace of the supplied exception.
|
static byte[] |
hexStringToByteArray(String hexString)
Restores a byte array that is encoded as a hex string.
|
static boolean |
isNullOrEmpty(String str)
Check if the string is empty or null.
|
static boolean |
isNumeric(String str)
Check if the string contains only digits.
|
static boolean |
isUuid(String str)
Determine if the supplied string is a valid
UUID. |
static String |
join(CharSequence delimiter,
int[] values)
Returns a new String composed of the supplied integer values joined together
with a copy of the specified
delimiter. |
static <T> String |
join(CharSequence delimiter,
Iterable<T> values)
Returns a new String composed of the supplied values joined together with a copy of the specified
delimiter. |
static <T> String |
join(CharSequence delimiter,
Iterable<T> values,
Function<T,String> conversion)
Returns a new String composed of the supplied values joined together with a copy of the specified
delimiter. |
static String |
justify(Strings.Justify justify,
String str,
int width,
char padWithChar)
Justify the contents of the string.
|
static String |
justifyCenter(String str,
int width,
char padWithChar)
Center the contents of the string.
|
static String |
justifyLeft(String str,
int width,
char padWithChar)
Left justify the contents of the string, ensuring that the supplied string begins at the first character and that the
resulting string is of the desired length.
|
protected static String |
justifyLeft(String str,
int width,
char padWithChar,
boolean trimWhitespace) |
static String |
justifyRight(String str,
int width,
char padWithChar)
Right justify the contents of the string, ensuring that the string ends at the last character.
|
static <T> List<T> |
listOf(String input,
Function<String,String[]> splitter,
Function<String,T> factory)
Generate the list of values that are included in the list.
|
static List<Pattern> |
listOfRegex(String input,
int regexFlags)
Generate the set of regular expression
Patterns that are specified in the string containing comma-separated
regular expressions. |
static String |
pad(String original,
int length,
char padChar)
Pad the string with the specific character to ensure the string is at least the specified length.
|
static String |
replaceVariables(String value,
Function<String,String> replacementsByVariableName)
Look in the supplied value for variables and replace them with values from the supplied lookup function.
|
static Function<String,String> |
replaceVariablesWith(Function<String,String> replacementsByVariableName)
Obtain a function that will replace variables in the supplied value with values from the supplied lookup function.
|
static String |
setLength(String original,
int length,
char padChar)
Set the length of the string, padding with the supplied character if the supplied string is shorter than desired, or
truncating the string if it is longer than desired.
|
static <T> Set<T> |
setOf(String input,
char delimiter,
Function<String,T> factory)
Generate the set of values that are included in the list delimited by the given delimiter.
|
static <T> Set<T> |
setOf(String input,
Function<String,String[]> splitter,
Function<String,T> factory)
Generate the set of values that are included in the list.
|
static <T> Set<T> |
setOf(String input,
Function<String,T> factory)
Generate the set of values that are included in the list separated by commas.
|
static Set<Pattern> |
setOfRegex(String input)
Generate the set of regular expression
Patterns that are specified in the string containing comma-separated
regular expressions. |
static Set<Pattern> |
setOfRegex(String input,
int regexFlags)
Generate the set of regular expression
Patterns that are specified in the string containing comma-separated
regular expressions. |
private static List<String> |
split(String str,
String splitter)
Split a string into pieces based on delimiters.
|
static List<String> |
splitLines(String content)
Split the supplied content into lines, returning each line as an element in the returned list.
|
static boolean |
startsWithIgnoreCase(String str,
String prefix)
Whether the given string begins with the given prefix, ignoring casing.
|
static String |
trim(String str)
Trim away any leading or trailing whitespace characters.
|
static String |
trim(String str,
Strings.CharacterPredicate predicate)
Trim away any leading or trailing characters that satisfy the supplied predicate
|
static String |
unquoteIdentifierPart(String identifierPart)
Unquotes the given identifier part (e.g.
|
private static final Pattern TIME_PATTERN
private static final String CURLY_PREFIX
private static final String CURLY_SUFFIX
private static final String VAR_DELIM
private static final String DEFAULT_DELIM
public static <T> Set<T> setOf(String input, Function<String,String[]> splitter, Function<String,T> factory)
input - the input stringsplitter - the function that splits the input into multiple items; may not be nullfactory - the factory for creating string items into filter matches; may not be nullpublic static <T> List<T> listOf(String input, Function<String,String[]> splitter, Function<String,T> factory)
input - the input stringsplitter - the function that splits the input into multiple items; may not be nullfactory - the factory for creating string items into filter matches; may not be nullpublic static <T> Set<T> setOf(String input, char delimiter, Function<String,T> factory)
input - the input stringdelimiter - the character used to delimit the items in the inputfactory - the factory for creating string items into filter matches; may not be nullpublic static <T> Set<T> setOf(String input, Function<String,T> factory)
input - the input stringfactory - the factory for creating string items into filter matches; may not be nullpublic static Set<Pattern> setOfRegex(String input, int regexFlags)
Patterns that are specified in the string containing comma-separated
regular expressions.input - the input string with comma-separated regular expressions. Comma can be escaped with backslash.Patterns included within the given string; never nullPatternSyntaxException - if the input includes an invalid regular expressionpublic static Set<Pattern> setOfRegex(String input)
Patterns that are specified in the string containing comma-separated
regular expressions.input - the input string with comma-separated regular expressions. Comma can be escaped with backslash.Patterns included within the given string; never nullPatternSyntaxException - if the input includes an invalid regular expressionpublic static List<Pattern> listOfRegex(String input, int regexFlags)
Patterns that are specified in the string containing comma-separated
regular expressions.input - the input string with comma-separated regular expressions. Comma can be escaped with backslash.regexFlags - the flags for compiling regular expressionsPatterns included in the list; never nullPatternSyntaxException - if the input includes an invalid regular expressionIllegalArgumentException - if bit values other than those corresponding to the defined
match flags are set in regexFlagspublic static List<String> splitLines(String content)
content - the string content that is to be splitpublic static int compareTo(CharSequence str1, CharSequence str2)
CharSequence instances.str1 - the first character sequence; may be nullstr2 - the second character sequence; may be nullpublic static boolean equalsIgnoreCase(String str1, String str2)
String instances are equal ignoring case.str1 - the first character sequence; may be nullstr2 - the second character sequence; may be nulltrue if both are null or if the two strings are equal to each other ignoring case, or false
otherwisepublic static String join(CharSequence delimiter, int[] values)
delimiter.delimiter - the delimiter that separates each elementvalues - the values to join together.String that is composed of the elements separated by the delimiterNullPointerException - If delimiter or elements is nullString.join(java.lang.CharSequence, java.lang.CharSequence...)public static <T> String join(CharSequence delimiter, Iterable<T> values)
delimiter.
All null values are simply ignored.delimiter - the delimiter that separates each elementvalues - the values to join together.String that is composed of the elements separated by the delimiterNullPointerException - If delimiter or elements is nullString.join(java.lang.CharSequence, java.lang.CharSequence...)public static <T> String join(CharSequence delimiter, Iterable<T> values, Function<T,String> conversion)
delimiter.delimiter - the delimiter that separates each elementvalues - the values to join together.conversion - the function that converts the supplied values into strings, or returns null if the value
is to be excludedString that is composed of the elements separated by the delimiterNullPointerException - If delimiter or elements is nullString.join(java.lang.CharSequence, java.lang.CharSequence...)public static String trim(String str)
This is semantically equivalent to String.trim() but instead uses trim(String, CharacterPredicate).
str - the string to be trimmed; may not be nulltrim(String,CharacterPredicate)public static String trim(String str, Strings.CharacterPredicate predicate)
str - the string to be trimmed; may not be nullpredicate - the predicate function; may not be nulltrim(String)public static String createString(char charToRepeat, int numberOfRepeats)
charToRepeat - the character to repeatnumberOfRepeats - the number of times the character is to repeat in the result; must be greater than 0public static String pad(String original, int length, char padChar)
original - the string to be padded; may not be nulllength - the minimum desired length; must be positivepadChar - the character to use for padding, if the supplied string is not long enoughjustifyLeft(String, int, char)public static String setLength(String original, int length, char padChar)
justifyLeft(String, int, char), this method does not
remove leading and trailing whitespace.original - the string for which the length is to be set; may not be nulllength - the desired length; must be positivepadChar - the character to use for padding, if the supplied string is not long enoughjustifyLeft(String, int, char)public static String justify(Strings.Justify justify, String str, int width, char padWithChar)
justify - the way in which the string is to be justifiedstr - the string to be right justified; if null, an empty string is usedwidth - the desired width of the string; must be positivepadWithChar - the character to use for padding, if neededpublic static String justifyRight(String str, int width, char padWithChar)
str - the string to be right justified; if null, an empty string is usedwidth - the desired width of the string; must be positivepadWithChar - the character to use for padding, if neededpublic static String justifyLeft(String str, int width, char padWithChar)
str - the string to be left justified; if null, an empty string is usedwidth - the desired width of the string; must be positivepadWithChar - the character to use for padding, if neededsetLength(String, int, char)protected static String justifyLeft(String str, int width, char padWithChar, boolean trimWhitespace)
public static String justifyCenter(String str, int width, char padWithChar)
str - the string to be left justified; if null, an empty string is usedwidth - the desired width of the string; must be positivepadWithChar - the character to use for padding, if neededsetLength(String, int, char)public static String getStackTrace(Throwable throwable)
throwable - the exception for which the stack trace is to be returnedpublic static Number asNumber(String value)
value - the string representation of a integer valuenull if the value is not a numberpublic static Number asNumber(String value, Supplier<Number> defaultValueProvider)
value - the string representation of a integer valuedefaultValueProvider - the function that returns a value to be used when the string value is null or cannot be parsed
as a number; may be null if no default value is to be usednull if the value is not a number and no default value is suppliedpublic static int asInt(String value, int defaultValue)
value - the string representation of a integer valuedefaultValue - the value to return if the string value is null or cannot be parsed as an intpublic static long asLong(String value, long defaultValue)
value - the string representation of a long valuedefaultValue - the value to return if the string value is null or cannot be parsed as a longpublic static double asDouble(String value, double defaultValue)
value - the string representation of a double valuedefaultValue - the value to return if the string value is null or cannot be parsed as a doublepublic static boolean asBoolean(String value, boolean defaultValue)
value - the string representation of a boolean valuedefaultValue - the value to return if the string value is null or cannot be parsed as a booleanpublic static Duration asDuration(String timeString)
Duration.Duration or null if null was passed.public static String duration(long durationInMillis)
HHH:MM:SS.mmm, where
durationInMillis - the duration in millisecondspublic static Function<String,String> replaceVariablesWith(Function<String,String> replacementsByVariableName)
Variables may appear anywhere within a string value, and multiple variables can be used within the same value. Variables take the form:
variable := '${' variableNames [ ':' defaultValue ] '}'
variableNames := variableName [ ',' variableNames ]
variableName := // any characters except ',' and ':' and '}'
defaultValue := // any characters except '}'
Note that variableName is the name used to look up a the property.
Notice that the syntax supports multiple variables. The logic will process the variables from let to right,
until an existing property is found. And at that point, it will stop and will not attempt to find values for the other
variables.
replacementsByVariableName - the function used to find the replacements for variable names; may not be nullpublic static String replaceVariables(String value, Function<String,String> replacementsByVariableName)
Variables may appear anywhere within a string value, and multiple variables can be used within the same value. Variables take the form:
variable := '${' variableNames [ ':' defaultValue ] '}'
variableNames := variableName [ ',' variableNames ]
variableName := // any characters except ',' and ':' and '}'
defaultValue := // any characters except '}'
Note that variableName is the name used to look up a the property.
Notice that the syntax supports multiple variables. The logic will process the variables from let to right,
until an existing property is found. And at that point, it will stop and will not attempt to find values for the other
variables.
value - the content in which the variables are to be found and replaced; may not be nullreplacementsByVariableName - the function used to find the replacements for variable names; may not be nullprivate static List<String> split(String str, String splitter)
str - Full stringsplitter - Characters to split onpublic static boolean isUuid(String str)
UUID.str - the string to evaluatetrue if the string is a valid representation of a UUID, or false otherwisepublic static boolean isNullOrEmpty(String str)
str - the string to checktrue if the string is empty or nullpublic static boolean isNumeric(String str)
str - the string to checktrue if only contains digitspublic static String unquoteIdentifierPart(String identifierPart)
public static byte[] hexStringToByteArray(String hexString)
public static boolean startsWithIgnoreCase(String str, String prefix)
public static String getBegin(String str, int length)
length characters of the given string.str - The string to get the begin fromlength - The length of the string to returnnull, if the given string is null, the first first
length characters of that string otherwise. If the string is
shorter than the given number of characters, the string itself will
be returned.Copyright © 2021 JBoss by Red Hat. All rights reserved.