Package org.threadly.util
Class StringUtils
- java.lang.Object
-
- org.threadly.util.StringUtils
-
public class StringUtils extends java.lang.Object
Some small utilities and constants around handling strings.- Since:
- 2.1.0
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
allCharsMatch(java.util.function.Predicate<java.lang.Character> p, java.lang.CharSequence s)
Check to see if all characters in a provided string match to a givenPredicate
.static boolean
anyCharsMatch(java.util.function.Predicate<java.lang.Character> p, java.lang.CharSequence s)
Check to see if any characters in a provided string match to a givenPredicate
.static java.lang.String
emptyToNull(java.lang.String input)
Converts an empty string into anull
.static boolean
isNullOrEmpty(java.lang.String input)
Check if the provided string is eithernull
or empty.static java.lang.String
makeRandomString(int length)
Produces a random string of the provided length.static java.util.Optional<java.lang.String>
nonEmptyOptional(java.lang.String input)
Simple utility function for returning anOptional
that if contents present are guaranteed to not be empty.static java.lang.String
nullToEmpty(java.lang.String input)
Makes sure a given string is notnull
.static java.lang.String
padEnd(java.lang.String sourceStr, int minLength, char padChar)
Pads the end of the provided string with the provided character until a minimum length is reached.static java.lang.String
padStart(java.lang.String sourceStr, int minLength, char padChar)
Pads the start of the provided string with the provided character until a minimum length is reached.
-
-
-
Method Detail
-
allCharsMatch
public static boolean allCharsMatch(java.util.function.Predicate<java.lang.Character> p, java.lang.CharSequence s)
Check to see if all characters in a provided string match to a givenPredicate
. This can be easily used when provided predicates fromCharacter
. For example providingCharacter::isLetter
for the predicate will verify all characters in the string are letters.- Parameters:
p
- Character tests
- CharSequence to check against- Returns:
true
if predicate returned true for every character in string- Since:
- 5.14
-
anyCharsMatch
public static boolean anyCharsMatch(java.util.function.Predicate<java.lang.Character> p, java.lang.CharSequence s)
Check to see if any characters in a provided string match to a givenPredicate
. This can be easily used when provided predicates fromCharacter
. For example providingCharacter::isDigit
for the predicate to see if this string contains any numbers.- Parameters:
p
- Character tests
- CharSequence to check against- Returns:
true
if predicate returned true for any characters in the string- Since:
- 5.14
-
nullToEmpty
public static java.lang.String nullToEmpty(java.lang.String input)
Makes sure a given string is notnull
. If it is notnull
, the provided string is immediately returned. If it ISnull
, then an empty string is returned.- Parameters:
input
- String which should be returned if notnull
- Returns:
- The original string if not
null
, otherwise an empty string - Since:
- 4.2.0
-
emptyToNull
public static java.lang.String emptyToNull(java.lang.String input)
Converts an empty string into anull
. If it is not empty ornull
, the provided string is immediately returned.- Parameters:
input
- String which should be returned if notnull
or empty- Returns:
- The original string if not empty, otherwise
null
- Since:
- 4.2.0
-
isNullOrEmpty
public static boolean isNullOrEmpty(java.lang.String input)
Check if the provided string is eithernull
or empty.- Parameters:
input
- String to check against- Returns:
true
if the provided string isnull
or has no content- Since:
- 4.2.0
-
nonEmptyOptional
public static java.util.Optional<java.lang.String> nonEmptyOptional(java.lang.String input)
Simple utility function for returning anOptional
that if contents present are guaranteed to not be empty. Basically taking the empty case into consideration in addition toOptional.ofNullable(Object)
's normalnull
check.- Parameters:
input
- String to be contained in returnedOptional
if not null or empty- Returns:
- Optional which if present contains a non-empty String
- Since:
- 5.4
-
padStart
public static java.lang.String padStart(java.lang.String sourceStr, int minLength, char padChar)
Pads the start of the provided string with the provided character until a minimum length is reached. If the provided string is greater than or equal to the minLength it will be returned without modification. If the provided string isnull
it will be returned as an empty string, padded to the minimum length.- Parameters:
sourceStr
- String to start from, this will be the end of the returned result stringminLength
- Minimum number of characters the returned string should bepadChar
- Character to pad on to the start of to reach the minimum length- Returns:
- A non-null string that is at least the length requested
- Since:
- 4.2.0
-
padEnd
public static java.lang.String padEnd(java.lang.String sourceStr, int minLength, char padChar)
Pads the end of the provided string with the provided character until a minimum length is reached. If the provided string is greater than or equal to the minLength it will be returned without modification. If the provided string isnull
it will be returned as an empty string, padded to the minimum length.- Parameters:
sourceStr
- String to start from, this will be the start of the returned result stringminLength
- Minimum number of characters the returned string should bepadChar
- Character to pad on to the end of to reach the minimum length- Returns:
- A non-null string that is at least the length requested
- Since:
- 4.2.0
-
makeRandomString
public static java.lang.String makeRandomString(int length)
Produces a random string of the provided length. This can be useful for unit testing, or any other time the string content is not important. The returned string will be comprised of only alphanumeric characters.- Parameters:
length
- Number of characters the resulting string should be.- Returns:
- A string comprised of random characters of the specified length
-
-