Class StringUtils2

java.lang.Object
org.jmxtrans.agent.util.StringUtils2

public class StringUtils2
extends Object
Yet another StringUtils class.
Author:
Cyrille Le Clerc
  • Method Details

    • delimitedStringToList

      public static List<String> delimitedStringToList​(@Nullable String delimitedString)
      Split given String. Delimiters are ",", ";" and "\n".
      Parameters:
      delimitedString -
      Returns:
      splitted string or null if given delimitedString is null
    • join

      public static String join​(@Nullable List<String> tokens, @Nonnull String delimiter)
      Join given tokens with given delimiter. Sample: tokens "com", "mycompany, "ecommerce", "server1" with delimiter "." returns "com.mycompany.ecommerce.server1".
      Parameters:
      tokens -
      delimiter -
      Returns:
      the joined tokens (null if given tokens is null
    • reverseTokens

      public static String reverseTokens​(@Nullable String str, @Nonnull String delimiter)

      Reverse tokens of given tokenized str.

      Sample: "server1.ecommerce.mycompany.com" returns "com.mycompany.ecommerce.server1".

      Parameters:
      str -
      delimiter -
      Returns:
      reversed string or null if given string is null
    • appendEscapedNonAlphaNumericChars

      public static void appendEscapedNonAlphaNumericChars​(@Nullable String str, boolean escapeDot, @Nonnull StringBuilder result)
      Escape all non 'a' to 'z','A' to 'Z', '0' to '9' and '-' with a '_'. '.' is escaped with a '_' if escapeDot is true.
      Parameters:
      str - the string to escape
      escapeDot - indicates whether '.' should be escaped into '_' or not.
      result - the StringBuilder in which the escaped string is appended
    • appendEscapedNonAlphaNumericChars

      public static void appendEscapedNonAlphaNumericChars​(@Nonnull String str, @Nonnull StringBuilder result)
      Escape all non 'a' to 'z','A' to 'Z', '0' to '9' and '-' with a '_'.
      Parameters:
      str - the string to escape
      result - the StringBuilder in which the escaped string is appended
    • isNullOrEmpty

      public static boolean isNullOrEmpty​(@Nullable String value)
    • abbreviate

      @Nonnull public static String abbreviate​(String str, int max)
      Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."
      Parameters:
      str - the String to abbreviate
      max - the max number of chars of the abbreviated String
      Returns:
      the abbreviated string
    • trimToEmpty

      @Nonnull public static String trimToEmpty​(@Nullable String str)
      if the given string is null, return "", else return String.trim().
      Parameters:
      str - the string to trim
      Returns:
      the trimmed string