Class StringUtils

java.lang.Object
net.logstash.logback.util.StringUtils

public class StringUtils extends Object
Operations on String that are null safe.

Largely inspired by Apache Commons Lang3.

Note: This class is for internal use only and subject to backward incompatible change at any time.

Author:
brenuart
  • Method Details

    • 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
    • trimToEmpty

      public static String trimToEmpty(String str)

      Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is null.

      The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

       StringUtils.trimToEmpty(null)          = ""
       StringUtils.trimToEmpty("")            = ""
       StringUtils.trimToEmpty("     ")       = ""
       StringUtils.trimToEmpty("abc")         = "abc"
       StringUtils.trimToEmpty("    abc    ") = "abc"
       
      Parameters:
      str - the String to be trimmed, may be null
      Returns:
      the trimmed String, or an empty String if null input
    • trimToNull

      public static String trimToNull(String str)

      Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null.

      The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

       StringUtils.trimToNull(null)          = null
       StringUtils.trimToNull("")            = null
       StringUtils.trimToNull("     ")       = null
       StringUtils.trimToNull("abc")         = "abc"
       StringUtils.trimToNull("    abc    ") = "abc"
       
      Parameters:
      str - the String to be trimmed, may be null
      Returns:
      the trimmed String, null if only chars <= 32, empty or null String input
    • 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
    • isBlank

      public static boolean isBlank(CharSequence cs)

      Checks if a CharSequence is empty (""), null or whitespace only.

      Whitespace is defined by Character.isWhitespace(char).

       StringUtils.isBlank(null)      = true
       StringUtils.isBlank("")        = true
       StringUtils.isBlank(" ")       = true
       StringUtils.isBlank("bob")     = false
       StringUtils.isBlank("  bob  ") = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is null, empty or whitespace only
    • length

      public static int length(CharSequence cs)
      Gets a CharSequence length or 0 if the CharSequence is null.
      Parameters:
      cs - a CharSequence or null
      Returns:
      CharSequence length or 0 if the CharSequence is null.
    • commaDelimitedListToStringArray

      public static String[] commaDelimitedListToStringArray(String str)
      Convert a comma delimited list into an array of strings.
      Parameters:
      str - the input String (potentially null or empty)
      Returns:
      an array of strings, or the empty array in case of empty input
      See Also:
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter)
      Take a String that is a delimited list and convert it into a String array.

      A single delimiter may consist of more than one character, but it will still be considered as a single delimiter string, rather than as a bunch of potential delimiter characters. Delimiter can be escaped by prefixing it with a backslash (\).

      Values are trimmed, and are added to the resulting array only if not blank. Therefore two consecutive delimiters are treated as a single delimiter.

      A null delimiter is treated as no delimiter and returns an array with the original str string as single element. An empty delimiter splits the input string at each character.

      A null input returns an empty array.

      Parameters:
      str - the input String (potentially null or empty)
      delimiter - the delimiter between elements
      Returns:
      an array of the tokens in the list