Package spark.utils

Class StringUtils

java.lang.Object
spark.utils.StringUtils

public abstract class StringUtils extends Object
Miscellaneous String utility methods.

Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of String utilities.

This class delivers some simple functionality that should really be provided by the core Java String and StringBuilder classes, such as the ability to replace(java.lang.String, java.lang.String, java.lang.String) all occurrences of a given substring in a target string. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.

  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • isBlank

      public static boolean isBlank(CharSequence cs)
    • isNotBlank

      public static boolean isNotBlank(CharSequence cs)
    • isEmpty

      public static boolean isEmpty(Object str)
      Check whether the given String is empty.

      This method accepts any Object as an argument, comparing it to null and the empty String. As a consequence, this method will never return true for a non-null non-String object.

      The Object signature is useful for general attribute handling code that commonly deals with Strings but generally has to iterate over Objects since attributes may e.g. be primitive value objects as well.

      Parameters:
      str - the candidate String
      Returns:
      if the String is empty
      Since:
      3.2.1
    • isNotEmpty

      public static boolean isNotEmpty(String str)
      Checks if the given String is not empty
      Parameters:
      str - the candidate String
      Returns:
      if the String is not empty
    • hasLength

      public static boolean hasLength(CharSequence str)
      Check that the given CharSequence is neither null nor of length 0.
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null and has length
    • hasLength

      public static boolean hasLength(String str)
      Check that the given String is neither null nor of length 0. Note: Will return 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:
    • replace

      public static String replace(String inString, String oldPattern, String newPattern)
      Replace all occurrences of a substring within a string with another string.
      Parameters:
      inString - String to examine
      oldPattern - String to replace
      newPattern - String to insert
      Returns:
      a String with the replacements
    • 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
    • getFilename

      public static String getFilename(String path)
      Extract the filename from the given path.
      Parameters:
      path - the file path (may be null)
      Returns:
      the extracted filename, or null if none
    • applyRelativePath

      public static String applyRelativePath(String path, String relativePath)
      Apply the given relative path to the given path, assuming standard Java folder separation (i.e. "/" separators).
      Parameters:
      path - the path to start from (usually a full file path)
      relativePath - the relative path to apply (relative to the full file path above)
      Returns:
      the full file path that results from applying the relative path
    • cleanPath

      public static String cleanPath(String path)
      Normalize the path by suppressing sequences like "path/.." and inner simple dots.

      The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.

      Parameters:
      path - the original path
      Returns:
      the normalized path
    • 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 (null if the passed-in Collection was null)
    • delimitedListToStringArray

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

      A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

      Parameters:
      str - the input String
      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
    • delimitedListToStringArray

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

      A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

      Parameters:
      str - the input String
      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
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)
      Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      coll - the Collection to display
      delim - the delimiter to use (probably a ",")
      prefix - the String to start each element with
      suffix - the String to end each element with
      Returns:
      the delimited String
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Collection<?> coll, String delim)
      Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      coll - the Collection to display
      delim - the delimiter to use (probably a ",")
      Returns:
      the delimited String
    • toString

      public static String toString(byte[] bytes, String encoding)
    • removeLeadingAndTrailingSlashesFrom

      public static String removeLeadingAndTrailingSlashesFrom(String string)