Class StringUtils

java.lang.Object
org.litebridgedb.commons.StringUtils

public final class StringUtils extends Object
Utility class for working with strings.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    abbreviate(@Nullable String str)
    Returns a lowercase string consisting of: the first letter of each "word" (a letter that follows a non-letter/digit), the first letter of each camelCase hump (an uppercase letter that follows a lowercase letter), and all digits found anywhere in the input. Non-letter/digit characters act as separators and are otherwise ignored.
    static String
    blankIfNull(@Nullable String str)
    Returns an empty string if the given string is null; otherwise, returns the input string.
    static boolean
    Checks if the given string contains only ASCII characters.
    static boolean
    isBlank(@Nullable String str)
    Checks if the given string is null, empty, or contains only whitespace characters.
    static boolean
    isEmpty(@Nullable String str)
    Checks if the given string is null or empty.
    static String
    requireNonBlank(@Nullable String str, String message)
    Ensures that the provided string is not null, empty, or composed solely of whitespace characters.
    static List<String>
    split(@Nullable String target, char delimiter)
    Splits a string by a single character.
    static List<String>
    split(@Nullable String target, char delimiter, int setSize, boolean padEmptyAtStart)
    Splits the given string by the specified delimiter, with optional padding and size constraints.
    static String[]
    splitArray(@Nullable String target, char delimiter, int setSize, boolean padEmptyAtStart)
    Splits a given string into an array of substrings based on a specified delimiter, with optional size constraints and padding for the resulting array.
    static String[]
    splitOnce(String str, char delimiter)
    Splits the given string into two parts by the first occurrence of the specified delimiter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isBlank

      public static boolean isBlank(@Nullable String str)
      Checks if the given string is null, empty, or contains only whitespace characters.
      Parameters:
      str - the string to check; may be null
      Returns:
      true if the string is null, empty, or contains only whitespace characters; false otherwise
    • isEmpty

      public static boolean isEmpty(@Nullable String str)
      Checks if the given string is null or empty.
      Parameters:
      str - the string to check; may be null
      Returns:
      true if the string is null or empty; false otherwise
    • splitOnce

      public static String[] splitOnce(String str, char delimiter)
      Splits the given string into two parts by the first occurrence of the specified delimiter. If the delimiter is not found, returns an array containing the original string as the only element.
      Parameters:
      str - the string to be split; must not be null
      delimiter - the character used as the delimiter for the split
      Returns:
      an array of two strings; the part before the delimiter and the part after it. If the delimiter is not found, returns a single-element array containing the original string.
    • split

      public static List<String> split(@Nullable String target, char delimiter)
      Splits a string by a single character. This is significantly faster than String.split() for single-character delimiters because it avoids regex.
      Parameters:
      target - The string to split.
      delimiter - The character to split by.
      Returns:
      A list of strings.
    • split

      public static List<String> split(@Nullable String target, char delimiter, int setSize, boolean padEmptyAtStart)
      Splits the given string by the specified delimiter, with optional padding and size constraints.
      Parameters:
      target - The string to split; may be null. If null, an empty list is returned.
      delimiter - The character to use as the delimiter for splitting the string.
      setSize - The desired number of elements in the result. If greater than 0, the resulting list will be padded or validated to match this size. If padding is required, empty strings will be used.
      padEmptyAtStart - If true, padding is added at the start of the list to meet the required size. If false, padding is added at the end of the list.
      Returns:
      A list of substrings resulting from the split. If the setSize parameter is provided and greater than 0, the list will be adjusted to match the desired size. If the input string is null, an empty list is returned. Throws IllegalArgumentException if the number of segments exceeds the specified setSize.
    • splitArray

      public static String[] splitArray(@Nullable String target, char delimiter, int setSize, boolean padEmptyAtStart)
      Splits a given string into an array of substrings based on a specified delimiter, with optional size constraints and padding for the resulting array.
      Parameters:
      target - The string to split; may be null. If null, an empty array is returned.
      delimiter - The character used as the delimiter for splitting the string.
      setSize - The desired number of elements in the result. If greater than 0, the resulting array will be padded or validated to match this size. If padding is required, empty strings will be used.
      padEmptyAtStart - If true, padding is added at the start of the array to meet the required size. If false, padding is added at the end of the array.
      Returns:
      An array of substrings resulting from the split. If the setSize parameter is provided and greater than 0, the array will be adjusted to match the desired size. If the input string is null, an empty array is returned.
      Throws:
      IllegalArgumentException - if the number of segments exceeds the specified setSize.
    • requireNonBlank

      public static String requireNonBlank(@Nullable String str, String message)
      Ensures that the provided string is not null, empty, or composed solely of whitespace characters. If the string is blank, an IllegalArgumentException is thrown with the provided message.
      Parameters:
      str - the string to check; may be null
      message - the exception message to use if the string is blank
      Returns:
      the provided string if it is not blank
      Throws:
      IllegalArgumentException - if the string is blank
    • blankIfNull

      public static String blankIfNull(@Nullable String str)
      Returns an empty string if the given string is null; otherwise, returns the input string.
      Parameters:
      str - the input string; may be null
      Returns:
      an empty string if str is null; otherwise, the input string
    • isAsciiOnly

      public static boolean isAsciiOnly(String str)
      Checks if the given string contains only ASCII characters.

      An ASCII character is defined as a character with a value less than or equal to 0x7F.

      Parameters:
      str - the string to check; must not be null
      Returns:
      true if the string contains only ASCII characters; false otherwise
    • abbreviate

      public static String abbreviate(@Nullable String str)
      Returns a lowercase string consisting of:
      • the first letter of each "word" (a letter that follows a non-letter/digit),
      • the first letter of each camelCase hump (an uppercase letter that follows a lowercase letter),
      • and all digits found anywhere in the input.
      Non-letter/digit characters act as separators and are otherwise ignored.