Class StringUtils

java.lang.Object
com.aspectran.utils.StringUtils

public abstract class StringUtils extends Object
Static utility methods pertaining to String or CharSequence instances.
  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • isEmpty

      public static boolean isEmpty(String str)
      Returns true if the given string is null or is the empty string.
      Parameters:
      str - a string reference to check
      Returns:
      true if the string is null or is the empty string
    • nullToEmpty

      public static String nullToEmpty(String str)
      Returns the given string if it is non-null; the empty string otherwise.
      Parameters:
      str - the string to test and possibly return
      Returns:
      string itself if it is non-null; "" if it is null
    • emptyToNull

      public static String emptyToNull(String str)
      Returns the given string if it is nonempty; null otherwise.
      Parameters:
      str - the string to test and possibly return
      Returns:
      string itself if it is nonempty; null if it is empty or null
    • hasLength

      public static boolean hasLength(CharSequence chars)
      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:
      chars - 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:
    • hasText

      public static boolean hasText(CharSequence chars)
      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:
      chars - 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:
    • containsWhitespace

      public static boolean containsWhitespace(CharSequence chars)
      Check whether the given CharSequence contains any whitespace characters.
      Parameters:
      chars - the CharSequence to check (maybe null)
      Returns:
      true if the CharSequence is not empty and contains at least 1 whitespace character
      See Also:
    • containsWhitespace

      public static boolean containsWhitespace(String str)
      Check whether the given String contains any whitespace characters.
      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not empty and contains at least 1 whitespace character
      See Also:
    • trimWhitespace

      public static String trimWhitespace(String str)
      Trim leading and trailing whitespace from the given String.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimAllWhitespace

      public static String trimAllWhitespace(String str)
      Trim all whitespace from the given String: leading, trailing, and in between characters.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimLeadingWhitespace

      public static String trimLeadingWhitespace(String str)
      Trim leading whitespace from the given String.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimTrailingWhitespace

      public static String trimTrailingWhitespace(String str)
      Trim trailing whitespace from the given String.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimLeadingCharacter

      public static String trimLeadingCharacter(String str, char leadingChar)
      Trim all occurrences of the supplied leading character from the given String.
      Parameters:
      str - the String to check
      leadingChar - the leading character to be trimmed
      Returns:
      the trimmed String
    • trimTrailingCharacter

      public static String trimTrailingCharacter(String str, char trailingChar)
      Trim all occurrences of the supplied trailing character from the given String.
      Parameters:
      str - the String to check
      trailingChar - the trailing character to be trimmed
      Returns:
      the trimmed String
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(String str, 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
      Returns:
      true if the String starts with the prefix, case-insensitive, or both null
      See Also:
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(String str, 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
      Returns:
      true if the String ends with the suffix, case-insensitive, or both null
      See Also:
    • startsWith

      public static boolean startsWith(String str, char prefix)
      Test if the given String starts with the specified prefix character.
      Parameters:
      str - the String to check
      prefix - the prefix character to look for
      Returns:
      true if the string starts with the specified prefix; otherwise false
      See Also:
    • endsWith

      public static boolean endsWith(String str, char suffix)
      Test if the given String ends with the specified prefix character.
      Parameters:
      str - the String to check
      suffix - the prefix character to look for
      Returns:
      true if the string ends with the specified suffix; otherwise false
      See Also:
    • replace

      public static String replace(String str, String search, String replacement)
      Replace all occurrences of a substring within a string with another string.
      Parameters:
      str - String to examine
      search - String to replace
      replacement - String to insert
      Returns:
      a String with the replacements
    • replace

      public static String replace(String str, String[] searchList, String[] replacementList)
      Replace all occurrences of a substring within a string with another string.
      Parameters:
      str - String to examine
      searchList - String array to replace
      replacementList - String array to insert
      Returns:
      a String with the replacements
    • replaceLast

      @NonNull public static String replaceLast(@NonNull String str, @NonNull String searchStr, @NonNull String replacement)
      Replace last occurrence of a string.
      Parameters:
      str - String to examine
      searchStr - String to replace
      replacement - String to insert
      Returns:
      a String with the replacements
    • repeat

      public static String repeat(char ch, int repeat)
      Returns padding using the specified delimiter repeated to a given length.
      Parameters:
      ch - character to repeat
      repeat - number of times to repeat char, negative treated as zero
      Returns:
      String with repeated character
    • divide

      @NonNull public static String[] divide(String str, String delim)
      Divide a String into a two-element array at the first occurrence of the delimiter. Does not include the delimiter in the result.
      Parameters:
      str - the string to divide (potentially null or empty)
      delim - to divide 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); if the delimiter wasn't found in the given input String, both elements of the array will be null.
    • split

      public static String[] split(String str, String delim)
      Returns an array of strings separated by the delimiter string.
      Parameters:
      str - the string to be separated
      delim - the delimiter
      Returns:
      an array, containing the splitted strings
    • split

      @NonNull public static String[] split(String str, String delim, int size)
      Returns an array of strings separated by the delimiter string.
      Parameters:
      str - the string to be separated
      delim - the delimiter
      size - the size of the array
      Returns:
      an array, containing the splitted strings
    • split

      public static String[] split(String str, char delim)
      Returns an array of strings separated by the delimiter string.
      Parameters:
      str - the string to be separated
      delim - the delimiter
      Returns:
      an array, containing the splitted strings
    • split

      @NonNull public static String[] split(String str, char delim, int size)
      Returns an array of strings separated by the delimiter string.
      Parameters:
      str - the string to be separated
      delim - the delimiter
      size - the size of the array
      Returns:
      an array, containing the splitted strings
    • splitWithComma

      public static String[] splitWithComma(String str)
      Convert a comma-delimited list (e.g., a row from a CSV file) into an array of strings.
      Parameters:
      str - the input String
      Returns:
      an array of strings, or the empty array in case of empty input
    • tokenize

      public static String[] tokenize(String str, String delimiters)
      Tokenize the given String into a String array via a StringTokenizer.
      Parameters:
      str - the String to tokenize
      delimiters - the delimiter characters
      Returns:
      an array of the tokens
    • tokenize

      public static String[] tokenize(String str, String delimiters, boolean trim)
      Tokenize the given String into a String array via a StringTokenizer.
      Parameters:
      str - the String to tokenize
      delimiters - the delimiter characters
      trim - trim the tokens via String's trim
      Returns:
      an array of the tokens
    • join

      public static String join(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
      delim - the delimiter to use (typically a ",")
      Returns:
      the delimited String
    • join

      public static String join(Collection<?> collection, String delim)
      Convert a Collection into a delimited String (e.g. CSV).

      Useful for toString() implementations.

      Parameters:
      collection - the collection
      delim - the delimiter to use (typically a ",")
      Returns:
      the delimited String
    • joinWithLines

      public static String joinWithLines(Object[] arr)
      Convert a String array into a delimited String by a system-dependent line separator.
      Parameters:
      arr - the array to display
      Returns:
      the delimited String
    • joinWithLines

      public static String joinWithLines(Collection<?> collection)
      Convert a Collection into a delimited String by a system-dependent line separator.
      Parameters:
      collection - the collection
      Returns:
      the delimited String
    • joinWithCommas

      public static String joinWithCommas(String[] arr)
      Convert a String array into a comma delimited String (i.e., CSV).
      Parameters:
      arr - the array to display
      Returns:
      the delimited String
    • joinWithCommas

      public static String joinWithCommas(Collection<?> collection)
      Convert a Collection into a comma delimited String (i.e., CSV).
      Parameters:
      collection - the collection
      Returns:
      the delimited String
    • 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 (potentially null or empty)
      Returns:
      the resulting 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 (potentially null or empty)
      Returns:
      the resulting String array
    • search

      public static int search(String str, String searchStr)
      Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.
      Parameters:
      str - the target string
      searchStr - the string to find
      Returns:
      the number of times the specified string was found
    • searchIgnoreCase

      public static int searchIgnoreCase(String str, String searchStr)
      Returns the number of times the specified string was found in the target string, or 0 if there is no specified string. When searching for the specified string, it is not case-sensitive.
      Parameters:
      str - the target string
      searchStr - the string to find
      Returns:
      the number of times the specified string was found
    • search

      public static int search(CharSequence chars, char searchChar)
      Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.
      Parameters:
      chars - the target string
      searchChar - the character to find
      Returns:
      the number of times the specified character was found
    • searchIgnoreCase

      public static int searchIgnoreCase(CharSequence chars, char searchChar)
      Returns the number of times the specified character was found in the target string, or 0 if there is no specified character. When searching for the specified character, it is not case-sensitive.
      Parameters:
      chars - the target string
      searchChar - the character to find
      Returns:
      the number of times the specified character was found
    • toHumanFriendlyByteSize

      @NonNull public static String toHumanFriendlyByteSize(long bytes)
      Convert byte size into human friendly format.
      Parameters:
      bytes - the number of bytes
      Returns:
      a human friendly byte size (includes units)
    • toMachineFriendlyByteSize

      public static long toMachineFriendlyByteSize(@NonNull String bytes)
      Convert byte size into machine friendly format.
      Parameters:
      bytes - the human friendly byte size (includes units)
      Returns:
      a number of bytes
      Throws:
      NumberFormatException - if failed parse given size