Class StringUtil

java.lang.Object
com.aquima.interactions.foundation.text.StringUtil

public final class StringUtil extends Object
Utility class providing various string operations.
Since:
5.0
Author:
O. Kerpershoek
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Comparator<String>
    Comparator that compares string in a non-case-sensitive manner.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    contains(String haystack, String needle)
    Checks whether haystack contains needle.
    static int
    Generates a hashCode for a string which is equal in Java as both in .NET C#.
    static boolean
    equal(String first, String second)
    This method compares strings case sensitive to determine if the strings are equal.
    static String
    insertParameters(String intermediate, String[] parameters)
    This method replaces tokens in the intermediate string with the parameters passed as second argument.
    static boolean
    isDigit(char c)
    This method returns true if the character is a number in the [0-9] range.
    static boolean
    Check if string is null or is an empty String.
    static String
    join(String[] array, String delimiter)
    Joins the specified string array into a single string, separating each element with the specified delimiter.
    static String
    join(List<String> stringList, String delimiter)
     
    static String
    joinIgnoreNull(String[] array, String delimiter)
    Joins the specified string array into a single string, separating each element with the specified delimiter.
    static String
    Returns a trimmed and toLowerCase String When isEmpty(String) returns true: null will be returned.
    static boolean
    This method tests if the specified string is not empty.
    static String
    notNull(String value)
    Returns the input string or the empty string if the input string is null.
    static String
    replaceAll(String input, String pattern, String replacement)
    Replaces all occurrences of a pattern in an input string with a provided replacement string.
    static String
    replaceAll(String input, Pattern pattern, String replacement)
     
    static String
    replaceInString(String original, String token, String newToken)
    This method will replace all occurrences of the first token with the new token in the original string.
    static String[]
    split(String str, String separator)
    The array returned by this method contains each substring of this string that is terminated by another substring or the end of the string.
    static String[]
    splitOnRegex(String str, String pattern)
    Same as split, except that this method takes a regular expression as separator
    static String
    toString(Object[] array)
    This method returns a pretty printed array.
    static String
    trim(String str)
    This method provides a null safe trim operation.
    static String
    valueOf(boolean b)
    Returns the string representation of the boolean argument.
    static String
    valueOf(char c)
    Returns the string representation of the char argument.
    static String
    valueOf(char[] data)
    Returns the string representation of the char array argument.
    static String
    valueOf(double d)
    Returns the string representation of the double argument.
    static String
    valueOf(float f)
    Returns the string representation of the float argument.
    static String
    valueOf(int i)
    Returns the string representation of the int argument.
    static String
    valueOf(long l)
    Returns the string representation of the long argument.
    static String
    Returns the string representation of the Object argument.

    Methods inherited from class java.lang.Object

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

    • CASE_INSENSITIVE_ORDER

      public static final Comparator<String> CASE_INSENSITIVE_ORDER
      Comparator that compares string in a non-case-sensitive manner. This variable can be used instead of String.CASE_INSENSITIVE_ORDER, as .NET does not have such a default comparator.
  • Method Details

    • normalize

      public static String normalize(String str)
      Returns a trimmed and toLowerCase String When isEmpty(String) returns true: null will be returned. This method may be used to generate case insensitive key strings for maps.
      Parameters:
      str - The string that should be normalized.
      Returns:
      The normalized variant of the provided string.
    • split

      public static String[] split(String str, String separator)
      The array returned by this method contains each substring of this string that is terminated by another substring or the end of the string. The substrings in the array are in the order in which they occur in this string. If the substring is not found in the input then the resulting array has just one element, namely this string. Empty entries including a trailing empty entry will be included in the result.
      Parameters:
      str - The string that should be split into multiple strings.
      separator - The separator that should be used to split the string on.
      Returns:
      Array containing the strings (without the separator) that resulted from the split operation.
    • splitOnRegex

      public static String[] splitOnRegex(String str, String pattern)
      Same as split, except that this method takes a regular expression as separator
      Parameters:
      str - The string that should be split into multiple strings
      pattern - the regular expression pattern to split the string on
      Returns:
      Array containing the strings that were split by the pattern. If the pattern was not matched, the array contains the original string. Note that trailing empty strings are discarded (limit = 0)
    • join

      public static String join(List<String> stringList, String delimiter)
    • join

      public static String join(String[] array, String delimiter)
      Joins the specified string array into a single string, separating each element with the specified delimiter. If the array is null, this method returns null in order to distinguish between a joined empty array and a joined null array. If the delimiter is null, the array elements will be concatenated without a delimiter.
      Parameters:
      array - the array to join (may be null)
      delimiter - the separator between elements (may be null)
      Returns:
      the concatenation of the array elements, separated by delimiter
      Since:
      9.7.1
    • joinIgnoreNull

      public static String joinIgnoreNull(String[] array, String delimiter)
      Joins the specified string array into a single string, separating each element with the specified delimiter. If the array is null, this method returns null in order to distinguish between a joined empty array and a joined null array. If the delimiter is null, the array elements will be concatenated without a delimiter.
      Parameters:
      array - the array to join (may be null)
      delimiter - the separator between elements (may be null)
      Returns:
      the concatenation of the array elements, separated by delimiter
      Since:
      12.4
    • replaceAll

      public static String replaceAll(String input, String pattern, String replacement)
      Replaces all occurrences of a pattern in an input string with a provided replacement string.
      Parameters:
      input - the input string
      pattern - the regular expression that is searched for in the input string
      replacement - the string which is replaced at every occurrence of the pattern in the input string Note that the replacement is first processed by Matcher.quoteReplacement(String) to turn it into a literal replacement string, so the \ and $ sign have no special meaning, as opposed to the default behavior of String.replaceAll(String, String)
      Returns:
      a string with all occurrences of pattern in the input string replaced with the replacement string. If the pattern is not found, the input string is returned.
    • replaceAll

      public static String replaceAll(String input, Pattern pattern, String replacement)
    • contains

      public static boolean contains(String haystack, String needle)
      Checks whether haystack contains needle. If either haystack or needle are null or empty, this method returns false.
      Parameters:
      haystack - the string to search in
      needle - the string to search for
      Returns:
      true if haystack contains needle, false otherwise.
    • trim

      public static String trim(String str)
      This method provides a null safe trim operation. When the passed argument was null, a null value will be returned, where as a non-null string will be trimmed of excess spaces.
      Parameters:
      str - The string that should be trimmed of excess (leading and trailing) spaces.
      Returns:
      The string without spaces at the begin or end, or null when a null value was provided.
    • equal

      public static boolean equal(String first, String second)
      This method compares strings case sensitive to determine if the strings are equal. If both strings are null or empty, the strings are also considered equal. Please note that leading/trailing spaces are ignored during comparison.
      Parameters:
      first - The first string that should be compared.
      second - The second string that should be compared.
      Returns:
      boolean indicating if the first and second string are equal.
    • isEmpty

      public static boolean isEmpty(String str)
      Check if string is null or is an empty String. A string is considered empty when it only consists of whitespace characters (including newline and tab characters).
      Parameters:
      str - The string that should be checked.
      Returns:
      Boolean indicating if the string was null or empty.
    • notEmpty

      public static boolean notEmpty(String str)
      This method tests if the specified string is not empty. A string is considered empty when it only consists of whitespace characters (including newline and tab characters).
      Parameters:
      str - The string that should be checked.
      Returns:
      Boolean indicating if the string is not empty (contains at least one non-whitespace character)
    • replaceInString

      public static String replaceInString(String original, String token, String newToken)
      This method will replace all occurrences of the first token with the new token in the original string.
      Parameters:
      original - The original string containing the tokens that should be replaced.
      token - The token that should be replaced.
      newToken - The replacement of the token.
      Returns:
      String with all the tokens replaced by new tokens.
    • valueOf

      public static String valueOf(boolean b)
      Returns the string representation of the boolean argument.
      Parameters:
      b - boolean
      Returns:
      if the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.
    • valueOf

      public static String valueOf(char c)
      Returns the string representation of the char argument.
      Parameters:
      c - char
      Returns:
      the string representation of the char argument.
    • valueOf

      public static String valueOf(char[] data)
      Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.
      Parameters:
      data - a char array
      Returns:
      a newly allocated string representing the same sequence of characters contained in the character array argument
    • valueOf

      public static String valueOf(double d)
      Returns the string representation of the double argument.
      Parameters:
      d - a double.
      Returns:
      a string representation of the double argument.
    • valueOf

      public static String valueOf(float f)
      Returns the string representation of the float argument.
      Parameters:
      f - a float.
      Returns:
      a string representation of the float argument.
    • valueOf

      public static String valueOf(int i)
      Returns the string representation of the int argument.
      Parameters:
      i - an int.
      Returns:
      a string representation of the int argument.
    • valueOf

      public static String valueOf(long l)
      Returns the string representation of the long argument.
      Parameters:
      l - a long.
      Returns:
      a string representation of the long argument.
    • valueOf

      public static String valueOf(Object obj)
      Returns the string representation of the Object argument.
      Parameters:
      obj - an Object.
      Returns:
      if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
    • insertParameters

      public static String insertParameters(String intermediate, String[] parameters)
      This method replaces tokens in the intermediate string with the parameters passed as second argument. A text containing:
       'abc {0}{1}{2}' with new String[] { "a", "b", "c", "d" } => 'abc abc'
       'abc {0}{1}{2}' with new String[] { "a", "b", "c" }      => 'abc abc'
       'abc {0}{1}{2}' with new String[] { "a", "b" }           => 'abc ab{2}'
       
      Parameters:
      intermediate - the input string which contains the parameter definitions
      parameters - the parameters
      Returns:
      String containing the parameter values at the appropriate places.
    • createHashCode

      public static int createHashCode(String string)
      Generates a hashCode for a string which is equal in Java as both in .NET C#.
      Parameters:
      string - the string to hash.
      Returns:
      the hashcode, 0 when the string is null
    • toString

      public static String toString(Object[] array)
      This method returns a pretty printed array. The toString method of all objects will be used to construct the pretty printed array. For example: String[] { "first", "second" } will return "[first, second]"
      Parameters:
      array - The array to pretty print (null will return "null")
      Returns:
      The pretty printed array.
    • isDigit

      public static boolean isDigit(char c)
      This method returns true if the character is a number in the [0-9] range.
      Parameters:
      c - The character to test.
      Returns:
      A boolean indicating if the character is a number in the [0-9] range.
    • notNull

      public static String notNull(String value)
      Returns the input string or the empty string if the input string is null.
      Parameters:
      value - the value
      Returns:
      the input string or the empty string if the input string is null.