java.lang.Object
nl.basjes.parse.useragent.utils.springframework.util.StringUtils

public abstract class StringUtils extends Object
Miscellaneous {link String} utility methods.

Mainly for internal use within the framework; consider Apache'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 {link String} and {link StringBuilder} classes. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.

Since:
16 April 2001
Author:
Rod Johnson, Juergen Hoeller, Keith Donald, Rob Harrop, Rick Evans, Arjen Poutsma, Sam Brannen, Brian Clozel
  • Method Details

    • hasLength

      public static boolean hasLength(@Nullable 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 #hasLength(CharSequence) see #hasText(String)
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(@Nullable String str, @Nullable String prefix)
      Test if the given String starts with the specified prefix, ignoring upper/lower case.
      Parameters:
      str - the String to check
      prefix - the prefix to look for see java.lang.String#startsWith
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(@Nullable String str, @Nullable String suffix)
      Test if the given String ends with the specified suffix, ignoring upper/lower case.
      Parameters:
      str - the String to check
      suffix - the suffix to look for see java.lang.String#endsWith
    • replace

      public static String replace(String inString, String oldPattern, @Nullable 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, @Nullable 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
    • capitalize

      public static String capitalize(String str)
      Capitalize a String, changing the first letter to upper case as per {link Character#toUpperCase(char)}. No other letters are changed.
      Parameters:
      str - the String to capitalize
      Returns:
      the capitalized String
    • getFilename

      @Nullable public static String getFilename(@Nullable String path)
      Extract the filename from the given Java resource path, e.g. "mypath/myfile.txt" → "myfile.txt".
      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 Java resource 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.

      NOTE that cleanPath should not be depended upon in a security context. Other mechanisms should be used to prevent path-traversal issues.

      Parameters:
      path - the original path
      Returns:
      the normalized path
    • toStringArray

      public static String[] toStringArray(@Nullable Collection<String> collection)
      Copy the given {link Collection} into a String array.

      The Collection must contain String elements only.

      Parameters:
      collection - the Collection to copy (potentially null or empty)
      Returns:
      the resulting String array
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(@Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
      Tokenize the given String into a String array via a {link 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 {link #delimitedListToStringArray}.

      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 {link 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 java.util.StringTokenizer see String#trim() see #delimitedListToStringArray
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(@Nullable String str, @Nullable 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, in contrast to {link #tokenizeToStringArray}.

      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 #tokenizeToStringArray
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(@Nullable String str, @Nullable String delimiter, @Nullable 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 a bunch of potential delimiter characters, in contrast to {link #tokenizeToStringArray}.

      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 #tokenizeToStringArray
    • collectionToDelimitedString

      public static String collectionToDelimitedString(@Nullable Collection<?> coll, String delim, String prefix, String suffix)
      Convert a {link Collection} to a delimited String (e.g. CSV).

      Useful for toString() implementations.

      Parameters:
      coll - the Collection to convert (potentially null or empty)
      delim - the delimiter to use (typically 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(@Nullable Collection<?> coll, String delim)
      Convert a Collection into a delimited String (e.g. CSV).

      Useful for toString() implementations.

      Parameters:
      coll - the Collection to convert (potentially null or empty)
      delim - the delimiter to use (typically a ",")
      Returns:
      the delimited String
    • arrayToDelimitedString

      public static String arrayToDelimitedString(@Nullable Object[] arr, String delim)
      Convert a String array into a delimited String (e.g. CSV).

      Useful for toString() implementations.

      Parameters:
      arr - the array to display (potentially null or empty)
      delim - the delimiter to use (typically a ",")
      Returns:
      the delimited String