Class StringUtils

java.lang.Object
com.gigaspaces.internal.utils.StringUtils

public abstract class StringUtils extends Object
Miscellaneous string utility methods. Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of string utilities.

This class delivers some simple functionality that should really be provided by the core Java String and StringBuffer classes, such as the ability to replace all occurrences of a given substring in a target string. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.

Author:
kimchy
  • Field Details

    • FOLDER_SEPARATOR

      public static final String FOLDER_SEPARATOR
      See Also:
    • NEW_LINE

      public static final String NEW_LINE
    • FORMAT_TIME_STAMP

      public static String FORMAT_TIME_STAMP
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • isEmpty

      public static boolean isEmpty(String str)
    • hasLength

      public static boolean hasLength(String str)
      Check if a String has length.

       StringUtils.hasLength(null) = false
       StringUtils.hasLength("") = false
       StringUtils.hasLength(" ") = true
       StringUtils.hasLength("Hello") = true
       
      Parameters:
      str - the String to check, may be null
      Returns:
      true if the String is not null and has length
    • hasText

      public static boolean hasText(String str)
      Check if a String has text. More specifically, returns true if the string not null, it's length is > 0, and it has 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:
      str - the String to check, may be null
      Returns:
      true if the String is not null, length > 0, and not whitespace only
      See Also:
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(String str1, String str2)
    • 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
      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
      See Also:
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(String str1, String str2)
    • countOccurrencesOf

      public static int countOccurrencesOf(String s, char c)
      Count the occurrences of the character c in string s.
      Parameters:
      s - String to scan.
      c - Character to find and count.
      Returns:
      Number of occurrences of c in s.
    • 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
      Returns:
      the String array (null if the Collection was null as well)
    • join

      public static String join(String[] array, String delimiter, int firstIndex, int count)
    • join

      public static String join(Collection<String> collection, String separator)
    • toList

      public static List<String> toList(String s, String delimiter)
    • toProperties

      public static Properties toProperties(String s, String propertyDelimiter, String keyValueSeparator)
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(String str, String delimiters)
      Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits empty tokens.

      The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

      Parameters:
      str - the String to tokenize
      delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).
      Returns:
      an array of the tokens
      See Also:
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
      Tokenize the given String into a String array via a StringTokenizer.

      The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

      Parameters:
      str - the String to tokenize
      delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)
      trimTokens - trim the tokens via String's trim
      ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).
      Returns:
      an array of the tokens
      See Also:
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter)
      Take a String which is a delimited list and convert it to a String array.

      A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

      Parameters:
      str - the input String
      delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
      Returns:
      an array of the tokens in the list
      See Also:
    • commaDelimitedListToStringArray

      public static String[] commaDelimitedListToStringArray(String str)
      Convert a CSV list into an array of Strings.
      Parameters:
      str - CSV list
      Returns:
      an array of Strings, or the empty array if s is null
    • arrayToDelimitedString

      public static String arrayToDelimitedString(Object[] arr, String delim)
      Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      arr - array to display. Elements may be of any type (toString will be called on each element).
      delim - delimiter to use (probably a ",")
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Collection<String> coll, String delim, String prefix, String suffix)
      Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      coll - Collection to display
      delim - delimiter to use (probably a ",")
      prefix - string to start each element with
      suffix - string to end each element with
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Collection<String> coll, String delim)
      Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      coll - Collection to display
      delim - delimiter to use (probably a ",")
    • arrayToCommaDelimitedString

      public static String arrayToCommaDelimitedString(Object[] arr)
      Convenience method to return a String array as a CSV String. E.g. useful for toString() implementations.
      Parameters:
      arr - array to display. Elements may be of any type (toString will be called on each element).
    • collectionToCommaDelimitedString

      public static String collectionToCommaDelimitedString(Collection<String> coll)
      Convenience method to return a Collection as a CSV String. E.g. useful for toString() implementations.
      Parameters:
      coll - Collection to display
    • convertArrayToSet

      public static Set<String> convertArrayToSet(String[] array)
      Transforms array of String objects to Set
      Parameters:
      array - array of String instances
      Returns:
      set
    • getParametersSet

      public static Set<String> getParametersSet(String locatorsStr)
      Transforms string with elements separated by "," to Set of elements
      Parameters:
      locatorsStr - elements delimetered by ","
      Returns:
      set
    • appendProperties

      public static void appendProperties(StringBuilder sb, Properties properties)
    • convertKeyValuePairsToArray

      public static String[] convertKeyValuePairsToArray(Map<String,String> value, String keyValueSeperator)
    • convertArrayToKeyValuePairs

      public static Map<String,String> convertArrayToKeyValuePairs(String[] pairs, String keyValueSeperator)
    • getTimeStamp

      public static String getTimeStamp()
    • getTimeStamp

      public static String getTimeStamp(long timeInMillis)
    • getSuffix

      public static String getSuffix(String s, String separator)
    • getFileName

      public static String getFileName(String path, boolean removeExtension)
    • getCurrentStackTrace

      public static String getCurrentStackTrace()
    • isStrictPrefix

      public static boolean isStrictPrefix(String s, String prefix)
    • trimToNull

      public static String trimToNull(String s)
    • resolvePlaceholders

      public static void resolvePlaceholders(Properties properties)
    • resolvePlaceholders

      public static void resolvePlaceholders(Properties properties, Map<String,String> environment)
    • resolvePlaceholders

      public static String resolvePlaceholders(String text)
    • resolvePlaceholders

      public static String resolvePlaceholders(String text, Map<String,String> environment)
    • resolvePlaceholders

      public static String resolvePlaceholders(String text, Map<String,String> environment, String prefix, String suffix)
    • extract

      public static String extract(String s, String prefix, String suffix)
    • parseStringAsBytes

      public static Long parseStringAsBytes(String property)
    • parseDurationAsMillis

      public static Long parseDurationAsMillis(String property)
    • parseTimeUnit

      public static TimeUnit parseTimeUnit(String s, TimeUnit defaultValue)
    • parseTimeUnit

      public static TimeUnit parseTimeUnit(String s)
    • leftPad

      public static String leftPad(String str, int n, char padding)
    • rightPad

      public static String rightPad(String str, int n, char padding)