Class SpringStringUtils

java.lang.Object
com.flyfish.oauth.utils.SpringStringUtils

public class SpringStringUtils extends Object
  • Constructor Details

    • SpringStringUtils

      public SpringStringUtils()
  • Method Details

    • split

      public static String[] split(String toSplit, String delimiter)
      Split a String at the first occurrence of the delimiter. Does not include the delimiter in the result.
      Parameters:
      toSplit - the string to split (potentially null or empty)
      delimiter - to split the string up with (potentially null or empty)
      Returns:
      a two element array with index 0 being before the delimiter, and index 1 being after the delimiter (neither element includes the delimiter); or null if the delimiter wasn't found in the given input String
    • hasText

      public static boolean hasText(CharSequence str)
      Check whether the given CharSequence contains actual text.

      More specifically, this method returns true if the CharSequence is not null, its length is greater than 0, and it contains at least one non-whitespace character.

      StringUtils.hasText(null) = false
      StringUtils.hasText("") = false
      StringUtils.hasText(" ") = false
      StringUtils.hasText("12345") = true
      StringUtils.hasText(" 12345 ") = true
      
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null, its length is greater than 0, and it does not contain whitespace only
      See Also:
    • hasText

      public static boolean hasText(String str)
      Check whether the given String contains actual text.

      More specifically, this method returns true if the String is not null, its length is greater than 0, and it contains at least one non-whitespace character.

      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not null, its length is greater than 0, and it does not contain whitespace only
      See Also:
    • hasLength

      public static boolean hasLength(CharSequence str)
      Check that the given CharSequence is neither null nor of length 0.

      Note: this method returns true for a CharSequence that purely consists of whitespace.

      StringUtils.hasLength(null) = false
      StringUtils.hasLength("") = false
      StringUtils.hasLength(" ") = true
      StringUtils.hasLength("Hello") = true
      
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null and has length
      See Also:
    • hasLength

      public static boolean hasLength(String str)
      Check that the given String is neither null nor of length 0.

      Note: this method returns true for a String that purely consists of whitespace.

      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not null and has length
      See Also:
    • toStringArray

      public static String[] toStringArray(Collection<String> collection)
      Copy the given Collection into a String array.

      The Collection must contain String elements only.

      Parameters:
      collection - the Collection to copy
      Returns:
      the String array
    • toStringArray

      public static String[] toStringArray(Enumeration<String> enumeration)
      Copy the given Enumeration into a String array. The Enumeration must contain String elements only.
      Parameters:
      enumeration - the Enumeration to copy
      Returns:
      the String array
    • 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 bunch of potential delimiter characters, in contrast to tokenizeToStringArray(String, String, boolean, boolean).

      Parameters:
      str - the input String (potentially null or empty)
      delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
      Returns:
      an array of the tokens in the list
      See Also:
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
      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 bunch of potential delimiter characters, in contrast to tokenizeToStringArray(String, String, boolean, boolean).

      Parameters:
      str - the input String (potentially null or empty)
      delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
      charsToDelete - a set of characters to delete; useful for deleting unwanted line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String
      Returns:
      an array of the tokens in the list
      See Also:
    • deleteAny

      public static String deleteAny(String inString, String charsToDelete)
      Delete any character in a given String.
      Parameters:
      inString - the original String
      charsToDelete - a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.
      Returns:
      the resulting String
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
      Tokenize the given String into a String array via a StringTokenizer.

      The given delimiters string can consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray(String, String).

      Parameters:
      str - the String to tokenize (potentially null or empty)
      delimiters - the delimiter characters, assembled as a String (each of the characters is individually considered as a delimiter)
      trimTokens - trim the tokens via String.trim()
      ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).
      Returns:
      an array of the tokens
      See Also:
    • splitArrayElementsIntoProperties

      public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter)
      Take an array of strings and split each element based on the given delimiter. A Properties instance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.

      Will trim both the key and value before adding them to the Properties.

      Parameters:
      array - the array to process
      delimiter - to split each element using (typically the equals symbol)
      Returns:
      a Properties instance representing the array contents, or null if the array to process was null or empty
    • splitArrayElementsIntoProperties

      public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter, String charsToDelete)
      Take an array of strings and split each element based on the given delimiter. A Properties instance is then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the value.

      Will trim both the key and value before adding them to the Properties instance.

      Parameters:
      array - the array to process
      delimiter - to split each element using (typically the equals symbol)
      charsToDelete - one or more characters to remove from each element prior to attempting the split operation (typically the quotation mark symbol), or null if no removal should occur
      Returns:
      a Properties instance representing the array contents, or null if the array to process was null or empty