Class StringUtils


  • public final class StringUtils
    extends Object

    Utility class providing handy methods to deal with Strings.

    Author:
    https://github.com/mcweba [Marc-Andre Weber]
    • Method Detail

      • isEmpty

        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
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is empty or null
      • isNotEmpty

        public 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
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is not empty and not null
      • isNotEmptyTrimmed

        public 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
         
        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is not empty and not null
      • trim

        public 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"
         
        Parameters:
        str - the String to be trimmed, may be null
        Returns:
        the trimmed string, null if null String input
      • getStringOrEmpty

        public 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"
         
        Parameters:
        inputString - the String to check, may be null
        Returns:
        the trimmed inputString or an empty ("") String
      • getStringOrDefault

        public 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"
         
        Parameters:
        inputString - the String to check, may be null
        def - the default value to return when inputString is null or empty
        Returns:
        the trimmed inputString or the provided default value
      • replaceWildcardConfigs

        public static String replaceWildcardConfigs​(String contentWithWildcards,
                                                    Map<String,​Object> properties)
        Returns a String with replaced wildcard values from the provided properties.

        See example below:

         Input Variant Plain string:
         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"
        
        
         Input Variant JSON non string attribute:
         contentWithWildcards: "attribute" : "$${number.replace}"
         properties: com.google.common.collect.ImmutableMap.of("number.replace", "1234")
        
         Output:
         "attribute" : 1234
         
        Parameters:
        contentWithWildcards - the String containing the wildcards to replace
        properties - the properties with the replacement values for the wildcards
        Returns:
        the String with replaced wildcard values. Returns the input String when input String or properties are null