Class StringUtils

java.lang.Object
org.apache.empire.commons.StringUtils

public class StringUtils extends Object
This class contains common functions for comparing and converting values of type String.
  • Field Details

    • EMPTY

      public static final String EMPTY
      Empty String
      See Also:
    • SPACE

      public static final String SPACE
      Single Space
      See Also:
    • NULL

      public static final String NULL
      Null String
      See Also:
    • DEFAULT_ITEM_SEPARATOR

      public static String DEFAULT_ITEM_SEPARATOR
      Default Item Separator
    • LIST_TEMPLATE

      public static String LIST_TEMPLATE
      Default List Template
    • TEMPLATE_SEP_CHAR

      public static char TEMPLATE_SEP_CHAR
  • Method Details

    • getObjectStringifier

      public static StringUtils.ObjectStringifier getObjectStringifier()
    • setObjectStringifier

      public static StringUtils.ObjectStringifier setObjectStringifier(StringUtils.ObjectStringifier stringifier)
    • toString

      public static String toString(Object value, String defValue)
      Converts a value to a string. If the value is null then the default value is returned.
      Parameters:
      value - the value to convert
      defValue - default value which to return if value is null
      Returns:
      returns a string for the object or the defValue if null
    • toString

      public static String toString(Object value)
      Converts a value to a string. If the value is null then null will be returned.
      Parameters:
      value - the value to convert
      Returns:
      returns a string for the object or null
    • valueOf

      public static String valueOf(Object value)
      Converts a value to a string. if the value is null an empty string is returned.
      Parameters:
      value - the value to convert
      Returns:
      returns a string for the object or an empty string if null
    • asString

      public static String asString(Object value)
      Converts a value to a string. Almost same as toString() but Lists and Arrays are wrapped with the standard list template (LIST_TEMPLATE) if the value is null an empty string is returned.
      Parameters:
      value - the value to convert
      Returns:
      returns a string for the object or an empty string if null
    • length

      public static int length(String value)
      Returns the length of a String
      Parameters:
      value - the string value
      Returns:
      length of the string value or 0
    • coalesce

      public static String coalesce(String preferred, String alternative)
      Returns the preferred String if it is not empty ot the alternative String otherwise.
      Parameters:
      preferred - the preferred String
      alternative - the alternative String if the preferred String is not valid
      Returns:
      the preferred String if it is not empty ot the alternative String otherwise
    • nullIfEmpty

      public static String nullIfEmpty(Object value)
      Returns null if the value supplied is null or an empty String.
      Parameters:
      value - the value to check
      Returns:
      null if the value supplied is null or an empty String or the value as a string otherwise
    • contains

      public static boolean contains(String value, String substring)
      Returns true if the given substring is part of the string provided by value
      Parameters:
      value - the value to check
      substring - the substring
      Returns:
      true if the given substring is part of the string provided by value
    • notContains

      public static boolean notContains(String value, String substring)
      Returns true if the given substring is part of the string provided by value
      Parameters:
      value - the value to check
      substring - the substring
      Returns:
      true if the given substring is part of the string provided by value
    • indexOfAny

      public static int indexOfAny(String value, char... chars)
      Returns the first index of any of the given characters in value starting from the beginning
      Parameters:
      value - the value to check
      chars - the characters to search
      Returns:
      the index
    • lastIndexOfAny

      public static int lastIndexOfAny(String value, char... chars)
      Returns the first index of any of the given characters in value starting from the end
      Parameters:
      value - the value to check
      chars - the characters to search
      Returns:
      the index
    • arrayToString

      public static String arrayToString(Object[] array, String template, String defItemValue, StringUtils.ObjectStringifier stringifier)
      Converts an array of objects to a string.
      Parameters:
      array - array of objects
      template - the list template or item separator
      defItemValue - the default item value
      stringifier - interface to convert an object to a string
      Returns:
      returns a String or null if the array is null or empty
    • arrayToString

      public static String arrayToString(Object[] array, String template, String defItemValue)
      Converts an array of objects to a string.
      Parameters:
      array - array of objects
      template - the list template or item separator
      defItemValue - the default item value
      Returns:
      returns a String or null if the array is null or empty
    • arrayToString

      public static String arrayToString(Object[] array, String separator)
      Converts an array of objects to a string.
      Parameters:
      array - array of objects
      separator - the separator to put between the object strings
      Returns:
      returns a String
    • arrayToString

      public static String arrayToString(Object[] array)
      Converts an array of objects to a string.
      Parameters:
      array - array of objects
      Returns:
      returns a String
    • estimateArrayBufferSize

      public static int estimateArrayBufferSize(Object[] array, StringUtils.ObjectStringifier stringifier, int separatorLength, int defValueLength, boolean ignoreEmpty)
      Estimates the buffer size needed to convert an Array into a String
      Parameters:
      array - the array
      stringifier - interface to convert an object to a string
      separatorLength - the separator length
      defValueLength - the default length
      ignoreEmpty - flag whether to ignore empty parts
      Returns:
      the estimated length of the collection parts
    • listToString

      public static String listToString(Collection<?> list, String template, String defItemValue, StringUtils.ObjectStringifier stringifier)
      Converts a list (Collection) of objects to a string.
      Parameters:
      list - the collection of objects
      template - the list template or item separator
      defItemValue - the default item value
      stringifier - interface to convert an object to a string
      Returns:
      returns a String or null if the list is null
    • listToString

      public static String listToString(Collection<?> list, String template, String defItemValue)
      Converts a list (Collection) of objects to a string.
      Parameters:
      list - the collection of objects
      template - the list template or item separator
      defItemValue - the default item value
      Returns:
      returns a String
    • listToString

      public static String listToString(Collection<?> list, String separator)
      Converts a list (Collection) of objects to a string.
      Parameters:
      list - the collection of objects
      separator - the separator to put between the object strings
      Returns:
      returns a String
    • listToString

      public static String listToString(Collection<?> list)
      Converts a list (Collection) of objects to a string.
      Parameters:
      list - the collection of objects
      Returns:
      returns a String
    • estimateListBufferSize

      public static int estimateListBufferSize(Collection<?> list, StringUtils.ObjectStringifier stringifier, int separatorLength, int defValueLength, boolean ignoreEmpty)
      Estimates the buffer size needed to convert a Collection into a String
      Parameters:
      list - the list to estimate
      stringifier - interface to convert an object to a string
      separatorLength - the separator length
      defValueLength - the default length
      ignoreEmpty - flag whether to ignore empty parts
      Returns:
      the estimated length of the collection parts
    • concatItems

      public static String concatItems(char separator, Object... params)
      Assembles a string from parts with a separator char
      Parameters:
      separator - the separator to put between the object strings
      params - array of objects
      Returns:
      returns a String
    • concat

      public static String concat(String... parts)
      Assembles a string from several parts
      Parameters:
      parts - the parts to concatenate
      Returns:
      returns a String
    • isEmpty

      public static boolean isEmpty(String s)
      Checks if a string is empty
      Parameters:
      s - the String to check
      Returns:
      true if s is empty or null
    • isNotEmpty

      public static boolean isNotEmpty(String s)
      Checks if a string is not null or empty
      Parameters:
      s - the string to validate
      Returns:
      true if valid
    • compareEqual

      public static boolean compareEqual(String s1, String s2, boolean ignoreCase)
      Compares two Strings with each other - either with or without character case. Both arguments may be null.
      Parameters:
      s1 - the first String
      s2 - the second String
      ignoreCase - whether to ignore the character casing or not
      Returns:
      true if the two strings supplied are equal
    • compareEqual

      public static boolean compareEqual(String s1, String s2)
      Compares two Strings with each other - either with or without character case. Both arguments may be null.
      Parameters:
      s1 - the first String
      s2 - the second String
      Returns:
      true if the two strings supplied are equal
    • compareNotEqual

      public static boolean compareNotEqual(String s1, String s2, boolean ignoreCase)
      Compares two Strings with each other - either with or without character case. Both arguments may be null.
      Parameters:
      s1 - the first String
      s2 - the second String
      ignoreCase - whether to ignore the character casing or not
      Returns:
      true if the two strings supplied are not equal
    • compareNotEqual

      public static boolean compareNotEqual(String s1, String s2)
      Compares two Strings with each other - either with or without character case. Both arguments may be null.
      Parameters:
      s1 - the first String
      s2 - the second String
      Returns:
      true if the two strings supplied are not equal
    • validate

      public static String validate(String s)
      Validates a given string. If the string is empty then null is returned. Otherwise the trimmed string is returned.
      Parameters:
      s - the string to validate
      Returns:
      the string or null if s was empty.
    • replace

      public static String replace(String source, String find, String replace)
      Replaces all occurrences of find in source by replace.
      Parameters:
      source - the original String.
      find - the String to be replaced
      replace - the replacement string
      Returns:
      a new string with all occurrences of find in source replaced by replace
    • replaceAll

      public static String replaceAll(String s, String find, String replace)
      Returns a String with all occurrences of from within orig replaced with to. If orig contains no occurrences of from, or if from is equal to to,orig itself is returned rather than a copy being made. If orig is null, null is returned.
      Parameters:
      s - the original String.
      find - the String to be replaced
      replace - the replacement string
      Returns:
      a new string with all occurrences of find in source replaced by replace
    • remove

      public static String remove(String s, String remove)
      Removes all occurrences of remove from s
      Parameters:
      s - the source string
      remove - the string to remove
      Returns:
      the result string
    • remove

      public static String remove(String s, char c)
      Removes all occurrences of c from s
      Parameters:
      s - the source string
      c - the character to remove
      Returns:
      the result string
    • removeBlanks

      public static String removeBlanks(String s)
      removes all blanks from s
      Parameters:
      s - the source string
      Returns:
      the result string
    • isCharBetween

      public static boolean isCharBetween(char c, char beg, char end)
      returns true if the character c is between the characters beg and end
      Parameters:
      c - the source character
      beg - the lower end character
      end - the higher end character
      Returns:
      true if the c is between beg and end, or false otherwise
    • isNumber

      public static boolean isNumber(char c)
      returns true if the character c is a number digit ('0'-'9')
      Parameters:
      c - the source character
      Returns:
      true if the c is between 0 and 9
    • isNumber

      public static boolean isNumber(String s)
      returns true if the string s is a number (contains only the characters 0 to 9)
      Parameters:
      s - the source string
      Returns:
      true if s contains only the characters 0 to 9
    • isNumeric

      public static boolean isNumeric(char c, boolean decimal)
      returns true if the character c is a numeric digit ('+' || '-' || '.' || ',' || '0'-'9')
      Parameters:
      c - the source character
      decimal - flag to indicate whether the decimal and grouping separators ('.' || ',') are allowed
      Returns:
      true if the c is a valid numeric character
    • isNumeric

      public static boolean isNumeric(String s, boolean decimal)
      returns true if the string s contains only numeric digits ('+' || '-' || '.' || ',' || '0'-'9')
      Parameters:
      s - the source string
      decimal - flag to indicate whether the decimal and grouping separators ('.' || ',') are allowed
      Returns:
      true if s contains only numeric digits
    • isUpper

      public static boolean isUpper(char c)
      returns true if the character c is an upper case character ('A'-'Z')
      Parameters:
      c - the character
      Returns:
      true if c is an upper case character
    • isUpper

      public static boolean isUpper(String s, int count)
      returns true if the first count characters of s are all upper case (or other non-case sensitive characters)
      Parameters:
      s - the source string
      count - number of characters to check
      Returns:
      true if the first count characters of s are all upper case
    • isLower

      public static boolean isLower(char c)
      returns true if the character c is an upper case character ('a'-'z')
      Parameters:
      c - the character
      Returns:
      true if c is an upper case character
    • isLower

      public static boolean isLower(String s, int count)
      returns true if the first count characters of s are all lower case (or other non-case sensitive characters)
      Parameters:
      s - the source string
      count - number of characters to check
      Returns:
      true if the first count characters of s are all lower case
    • toUpper

      public static String toUpper(String s, int count)
      makes the first n characters of s upper case
      Parameters:
      s - the source string
      count - the number of characters to convert
      Returns:
      the result string
    • toLower

      public static String toLower(String s, int count)
      makes the first n characters of s lower case
      Parameters:
      s - the source string
      count - the number of characters to convert
      Returns:
      the result string
    • truncate

      public static String truncate(String s, int maxChar)
      truncates a string to a maximum number of chars
      Parameters:
      s - the source string
      maxChar - the maximum number of chars
      Returns:
      the result string
    • padLeft

      public static String padLeft(String s, int size, char padChar)
      Pads a String to the left
      Parameters:
      s - the string to pad
      size - the desired size
      padChar - the padding char
      Returns:
      the padded string
    • padRight

      public static String padRight(String s, int size, char padChar)
      Pads a String to the right
      Parameters:
      s - the string to pad
      size - the desired size
      padChar - the padding char
      Returns:
      the padded string
    • toCamelCase

      public static String toCamelCase(String text, boolean firstCharUpper)
      Converts a String to camel case Words must be separated by underscore
      Parameters:
      text - the string to convert
      firstCharUpper - flag wether the first character should be upper case (true) or lower case (false)
      Returns:
      the camel case string