public final class StringUtils extends Object
Utility class providing handy methods to deal with Strings.
| Modifier and Type | Method and Description |
|---|---|
static String |
getStringOrDefault(String inputString,
String def)
Returns a trimmed String containing the provided value or the provided default String.
|
static String |
getStringOrEmpty(String inputString)
Returns a trimmed String containing the provided value or an empty ("") String.
|
static boolean |
isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static boolean |
isNotEmpty(CharSequence cs)
Checks if a CharSequence is not empty ("") and not null.
|
static boolean |
isNotEmptyTrimmed(String str)
Checks if a trimmed String is not empty ("") and not null.
|
static String |
replaceWildcardConfigs(String contentWithWildcards,
Map<String,Object> properties)
Returns a String with replaced wildcard values from the provided properties.
|
static String |
trim(String str)
Removes control characters (char <= 32) from both ends of this String, handling
null by returning null. |
public static boolean isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
cs - the CharSequence to check, may be nulltrue if the CharSequence is empty or nullpublic static boolean isNotEmpty(CharSequence cs)
Checks if a CharSequence is not empty ("") and not null.
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
cs - the CharSequence to check, may be nulltrue if the CharSequence is not empty and not nullpublic static boolean isNotEmptyTrimmed(String str)
Checks if a trimmed String is not empty ("") and not null.
StringUtils.isNotEmptyTrimmed(null) = false
StringUtils.isNotEmptyTrimmed("") = false
StringUtils.isNotEmptyTrimmed(" ") = false
StringUtils.isNotEmptyTrimmed("bob") = true
StringUtils.isNotEmptyTrimmed(" bob ") = true
str - the String to check, may be nulltrue if the String is not empty and not nullpublic static String trim(String str)
Removes control characters (char <= 32) from both ends of this String, handling null by returning null.
The String is trimmed using String.trim(). Trim removes start and end characters <= 32.
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
str - the String to be trimmed, may be nullnull if null String inputpublic static String getStringOrEmpty(String inputString)
Returns a trimmed String containing the provided value or an empty ("") String.
StringUtils.getStringOrEmpty(null) = ""
StringUtils.getStringOrEmpty("") = ""
StringUtils.getStringOrEmpty(" ") = ""
StringUtils.getStringOrEmpty("bob") = "bob"
StringUtils.getStringOrEmpty(" bob ") = "bob"
inputString - the String to check, may be nullpublic static String getStringOrDefault(String inputString, String def)
Returns a trimmed String containing the provided value or the provided default String.
StringUtils.getStringOrDefault(null, "bob") = "bob"
StringUtils.getStringOrDefault("", "bob") = "bob"
StringUtils.getStringOrDefault(" ", "bob") = "bob"
StringUtils.getStringOrDefault("alice", "bob") = "alice"
StringUtils.getStringOrDefault(" alice ", "bob") = "alice"
inputString - the String to check, may be nulldef - the default value to return when inputString is null or emptypublic static String replaceWildcardConfigs(String contentWithWildcards, Map<String,Object> properties)
Input:
contentWithWildcards: "This is a very ${adjective} helper method"
properties: com.google.common.collect.ImmutableMap.of("adjective", "nice")
Output:
"This is a very nice helper method"
contentWithWildcards - the String containing the wildcards to replaceproperties - the properties with the replacement values for the wildcardsnullCopyright © 2016–2018. All rights reserved.