Class StringUtil

java.lang.Object
net.sf.basedb.core.StringUtil

public class StringUtil extends Object
Utility methods that will make it easier to implement data validation for string attributes.
Version:
2.0
Author:
Nicklas
  • Constructor Details

    • StringUtil

      public StringUtil()
  • Method Details

    • setNullableString

      public static String setNullableString(String value, String name, int maxLength) throws StringTooLongException
      Trim and check the length of a string. Null values are allowed.
      Parameters:
      value - The string to check
      name - The name of the attribute to use if an exception is thrown
      maxLength - The maximum length of the string
      Returns:
      The original string, trimmed from white-space at the beginning and end
      Throws:
      StringTooLongException - If the string is too long
    • setNotNullString

      public static String setNotNullString(String value, String name, int maxLength) throws InvalidUseOfNullException, StringTooLongException
      Trim and check the length of a string. Null values are not allowed.
      Parameters:
      value - The string to check
      name - The name of the attribute to use if an exception is thrown
      maxLength - The maximum length of the string
      Returns:
      The original string, trimmed from white-space at the beginning and end
      Throws:
      StringTooLongException - If the string is too long
      InvalidUseOfNullException - If the string is null
    • isEqualOrNull

      public static boolean isEqualOrNull(String s1, String s2)
      Check if two strings are equal or both are null.
      Parameters:
      s1 - The first string
      s2 - The second string
      Returns:
      TRUE if the strings are equal or both are null, FALSE otherwise
    • isEqualOrNull

      public static boolean isEqualOrNull(String s1, String s2, boolean caseInsensitive)
      Check if two strings are equal or both are null.
      Parameters:
      s1 - The first string
      s2 - The second string
      caseInsensitive - TRUE if the comparison should ignore case, FALSE otherwise
      Returns:
      TRUE if the strings are equal or both are null, FALSE otherwise
      Since:
      2.4
    • join

      public static final String join(Collection<?> values, String deliminator, boolean skipNull)
      Join a collection of values to a string, using the specified delimiator.
      Parameters:
      values - The values to join
      deliminator - The deliminator between the values
      skipNull - If null values should be skipper or not
      Returns:
      The resulting string
    • trimString

      public static final String trimString(String s, int length)
      Trim a string to a maximum length. String longer than the specified length will be cut off and three dots are appended. If no trimming is needed the original string is returned.
      Parameters:
      s - The string to trim
      length - The maximum length of the string
      Returns:
      The trimmed string
    • trimStringMiddle

      public static final String trimStringMiddle(String s, int length)
      Trim a string to a maximum length. Strings longer than the specified length will have text cut-out from the middle and replaced by three dots. If no trimming is needed the original string is returned.
      Parameters:
      s - The string to trim
      length - The maximum length of the string
      Returns:
      The trimmed string
      Since:
      2.14
    • tokenize

      public static final String[] tokenize(String s, String regexp, boolean notInsideQuotes)
      Tokenize a string by splitting it at positions matching the regular expression, optionally ignoring matches that are inside quotes.
      Parameters:
      s - The string to tokenize
      regexp - The regular expression for matching split sites
      notInsideQuotes - If TRUE, the regexp is not matched inside quotes
      Returns:
      An array with the elements of the array
      Since:
      2.15
    • tokenize

      public static final String[] tokenize(String s, String regexp, boolean notInsideQuotes, boolean removeQuotes)
      Tokenize a string by splitting it at positions matching the regular expression. Matches that are inside quotes can optionally be ignored. Elements with quotes can optionally have the quotes removed.
      Parameters:
      s - The string to tokenize
      regexp - The regular expression for matching split sites
      notInsideQuotes - If TRUE, the regexp is not matched inside quotes
      removeQuotes - If TRUE, remove quotes from all elements before returning the result
      Returns:
      An array with the elements of the array
      Since:
      2.17
    • getCommonPrefix

      public static final String getCommonPrefix(Collection<String> strings)
      Find the longest common prefix for a collection of strings. If the collection is null, empty or contains at least one null element null is returned. If the given strings doesn't have a common prefix the empty string is returned.
      Parameters:
      strings - A collection of string objects
      Returns:
      The longes string such that String.startsWith(String) returns true for all strings in the given collection
      Since:
      2.17
    • decapitalize

      public static final String decapitalize(String value)
      Decapitalize the string by making sure the first character is lower-case. All other characters are left as they are. If the string is empty, the empty string is returned. If the string is null, null is returned.
      Since:
      3.2
    • coalesce

      public static final String coalesce(String... values)
      Return the first non-null value (or null if all values are null).
      Since:
      3.19