Class StringUtils

java.lang.Object
org.jppf.utils.StringUtils

public final class StringUtils extends Object
This class provides a set of utility methods for manipulating strings.
Author:
Laurent Cohen
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Charset
    Charset instance for UTF-8 encoding.
    static final Object[]
    Constant for an empty array of Objects.
    static final URL[]
    Constant for an empty array of URLs.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> String
    arrayToString(String sep, String prefix, String suffix, T... array)
    Get a String representation of an array of any type.
    static <T> String
    arrayToString(T... array)
    Get a String representation of an array of any type.
    static String
    build(Object... args)
    Build a string made of the specified tokens.
    static String
    buildString(int[] ports)
    Convert an array of int values into a space-separated string.
    static String
    buildString(String sep, String prefix, String suffix, int[] array)
    Get a String representation of an array of any type.
    static String
    Transform a file path into a URL in non URL-ncoded form.
    static boolean
    hasOneOf(String source, boolean ignoreCase, String... values)
    Determine whether the specified source string contains one of the specified values.
    static boolean
    isOneOf(String source, boolean ignoreCase, String... values)
    Determine whether the specified source string is equal to one of the specified values.
    static String
    maskPassword(String password)
    Mask the specified password.
    static String
    padLeft(String source, char padChar, int maxLen)
    Format a string so that it fits into a string of specified length.
    If the string is longer than the specified length, then characters on the left are truncated, otherwise the specified character is appended to the result on the left to obtain the appropriate length.
    static String
    padLeft(String source, char padChar, int maxLen, boolean truncate)
    Format a string so that it fits into a string of specified length.
    If the string is longer than the specified length, then characters on the left are truncated, otherwise the specified character is appended to the result on the left to obtain the appropriate length.
    static String
    padRight(String source, char padChar, int maxLen)
    Pads a string on the right side with a given character If the string is longer than the specified length, then characters on the right are truncated, otherwise the specified character is appended to the result on the right to obtain the appropriate length.
    static String
    padRight(String source, char padChar, int maxLen, boolean truncate)
    Pads a string on the right side with a given character If the string is longer than the specified length, then characters on the right are truncated, otherwise the specified character is appended to the result on the right to obtain the appropriate length.
    static int[]
    Parse an array of port numbers from a string containing a list of space-separated port numbers.
    static Number
    parseNumber(String source, Number def)
    Parse a Number from a String.
    static String[]
    parseStringArray(String source, String separator)
    Parse the specified source string into a list of strings according to the specified separator.
    static String[]
    parseStringArray(String source, String separator, boolean regex)
    Parse the specified source string into a list of strings according to the specified separator.
    static List<String>
    parseStrings(String source, String separator)
    Parse the specified source string into a list of strings according to the specified separator.
    static List<String>
    parseStrings(String source, String separator, boolean regex)
    Parse the specified source string into a list of strings according to the specified separator.
    static String
    Print a top-down representation of a class loader hierarchy into a string.
    static boolean
    startsWithOneOf(String source, boolean ignoreCase, String... values)
    Determine whether the specified source string starts with one of the specified values.
    static String
    toHexString(byte[] bytes)
    Convert an array of bytes into a string of hexadecimal numbers.
    static String
    toHexString(byte[] bytes, int start, int length, String sep)
    Convert a part of an array of bytes, into a string of hexadecimal numbers.
    static String
    Return a String in the format <object class name>@hashcode for the specified object.
    static String
    Print a representation of a URLClassLoader into a string.
    static String
    toStringDuration(long duration)
    Transform a duration in milliseconds into a string with hours, minutes, seconds and milliseconds..
    static String
    unquote(String source)
    Remove the leading and trailing quotes form the source string.

    Methods inherited from class java.lang.Object

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

    • UTF_8

      public static final Charset UTF_8
      Charset instance for UTF-8 encoding.
    • ZERO_OBJECT

      public static final Object[] ZERO_OBJECT
      Constant for an empty array of Objects.
    • ZERO_URL

      public static final URL[] ZERO_URL
      Constant for an empty array of URLs.
  • Method Details

    • padLeft

      public static String padLeft(String source, char padChar, int maxLen)
      Format a string so that it fits into a string of specified length.
      If the string is longer than the specified length, then characters on the left are truncated, otherwise the specified character is appended to the result on the left to obtain the appropriate length.
      Parameters:
      source - the string to format; if null, it is considered an empty string.
      padChar - the character used to fill the result up to the specified length.
      maxLen - the length of the formatted string.
      Returns:
      a string formatted to the specified length.
    • padLeft

      public static String padLeft(String source, char padChar, int maxLen, boolean truncate)
      Format a string so that it fits into a string of specified length.
      If the string is longer than the specified length, then characters on the left are truncated, otherwise the specified character is appended to the result on the left to obtain the appropriate length.
      Parameters:
      source - the string to format; if null, it is considered an empty string.
      padChar - the character used to fill the result up to the specified length.
      maxLen - the length of the formatted string.
      truncate - if true, then truncate the string if its length is greater than maxLen.
      Returns:
      a string formatted to the specified length.
    • padRight

      public static String padRight(String source, char padChar, int maxLen)
      Pads a string on the right side with a given character If the string is longer than the specified length, then characters on the right are truncated, otherwise the specified character is appended to the result on the right to obtain the appropriate length.
      Parameters:
      source - the string to pad to the right
      padChar - the character used for padding
      maxLen - the length to pad the string up to if its length is greater than the padding length
      Returns:
      the padded (or truncated) string
    • padRight

      public static String padRight(String source, char padChar, int maxLen, boolean truncate)
      Pads a string on the right side with a given character If the string is longer than the specified length, then characters on the right are truncated, otherwise the specified character is appended to the result on the right to obtain the appropriate length.
      Parameters:
      source - the string to pad to the right
      padChar - the character used for padding
      maxLen - the length to pad the string up to if its length is greater than the padding length
      truncate - if true, then truncate the string if its length is greater than maxLen.
      Returns:
      the padded (or truncated) string
    • toHexString

      public static String toHexString(byte[] bytes)
      Convert an array of bytes into a string of hexadecimal numbers.
      Parameters:
      bytes - the array that contains the sequence of byte values to convert.
      Returns:
      the converted bytes as a string of space-separated hexadecimal numbers.
    • toHexString

      public static String toHexString(byte[] bytes, int start, int length, String sep)
      Convert a part of an array of bytes, into a string of hexadecimal numbers. The hex numbers may or may not be separated, depending on the value of the sep parameter.
      Parameters:
      bytes - the array that contains the sequence of byte values to convert.
      start - the index to start at in the byte array.
      length - the number of bytes to convert in the array.
      sep - the separator between hexadecimal numbers in the resulting string. If null, then no separator is used.
      Returns:
      the converted bytes as a string of space-separated hexadecimal numbers.
    • toStringDuration

      public static String toStringDuration(long duration)
      Transform a duration in milliseconds into a string with hours, minutes, seconds and milliseconds..
      Parameters:
      duration - the duration to transform, expressed in milliseconds.
      Returns:
      a string specifying the duration in terms of hours, minutes, seconds and milliseconds.
    • arrayToString

      @SafeVarargs public static <T> String arrayToString(T... array)
      Get a String representation of an array of any type.
      Type Parameters:
      T - the type of the array.
      Parameters:
      array - the array from which to build a string representation.
      Returns:
      the array's content as a string.
    • arrayToString

      @SafeVarargs public static <T> String arrayToString(String sep, String prefix, String suffix, T... array)
      Get a String representation of an array of any type.
      Type Parameters:
      T - the type of the array.
      Parameters:
      array - the array from which to build a string representation.
      sep - the separator to use for values. If null, no separator is used.
      prefix - the prefix to use at the start of the resulting string. If null, no prefix is used.
      suffix - the suffix to use at the end of the resulting string. If null, no suffix is used.
      Returns:
      the array's content as a string.
    • parseIntValues

      public static int[] parseIntValues(String s)
      Parse an array of port numbers from a string containing a list of space-separated port numbers.
      Parameters:
      s - list of space-separated port numbers
      Returns:
      an array of int port numbers.
    • buildString

      public static String buildString(int[] ports)
      Convert an array of int values into a space-separated string.
      Parameters:
      ports - list of port numbers
      Returns:
      a space-separated list of ports.
    • buildString

      public static String buildString(String sep, String prefix, String suffix, int[] array)
      Get a String representation of an array of any type.
      Parameters:
      array - the array from which to build a string representation.
      sep - the separator to use for values. If null, no separator is used.
      prefix - the prefix to use at the start of the resulting string. If null, no prefix is used.
      suffix - the suffix to use at the end of the resulting string. If null, no suffix is used.
      Returns:
      the array's content as a string.
    • build

      public static String build(Object... args)
      Build a string made of the specified tokens.
      Parameters:
      args - the tokens composing the string.
      Returns:
      the concatenation of the string values of the tokens.
    • startsWithOneOf

      public static boolean startsWithOneOf(String source, boolean ignoreCase, String... values)
      Determine whether the specified source string starts with one of the specified values.
      Parameters:
      source - the string to match with the values.
      ignoreCase - specifies whether case should be ignore in the string matching.
      values - the values to match the source with.
      Returns:
      true if the source matches one of the values, false otherwise.
    • isOneOf

      public static boolean isOneOf(String source, boolean ignoreCase, String... values)
      Determine whether the specified source string is equal to one of the specified values.
      Parameters:
      source - the string to match with the values.
      ignoreCase - specifies whether case should be ignore in the string matching.
      values - the values to match the source with.
      Returns:
      true if the source matches one of the values, false otherwise.
    • hasOneOf

      public static boolean hasOneOf(String source, boolean ignoreCase, String... values)
      Determine whether the specified source string contains one of the specified values.
      Parameters:
      source - the string to match with the values.
      ignoreCase - specifies whether case should be ignore in the string matching.
      values - the values to match the source with.
      Returns:
      true if the source matches one of the values, false otherwise.
    • printClassLoaderHierarchy

      public static String printClassLoaderHierarchy(ClassLoader leafClassLoader)
      Print a top-down representation of a class loader hierarchy into a string.
      Parameters:
      leafClassLoader - the class loader at the bottom of the hierarchy.
      Returns:
      a string representation of the class loader hierarchy.
    • toString

      public static String toString(URLClassLoader cl)
      Print a representation of a URLClassLoader into a string. The resulting string includes the class loader's classpath.
      Parameters:
      cl - the classloader to print.
      Returns:
      a string representation of the input class loader.
    • parseNumber

      public static Number parseNumber(String source, Number def)
      Parse a Number from a String.
      Parameters:
      source - the string to parse.
      def - the default value to return if the source cannot be parsed.
      Returns:
      the source parsed as a number or def if it could not be parsed as a number.
    • toIdentityString

      public static String toIdentityString(Object obj)
      Return a String in the format <object class name>@hashcode for the specified object.
      Parameters:
      obj - the object for which to get a string.
      Returns:
      an identity string, or "null" if the object is null.
    • parseStrings

      public static List<String> parseStrings(String source, String separator)
      Parse the specified source string into a list of strings according to the specified separator.
      Parameters:
      source - the string to parse.
      separator - the delimiter for the resulting strings; it can be a regex.
      Returns:
      a list of strings, possibly empty but never null;
    • parseStrings

      public static List<String> parseStrings(String source, String separator, boolean regex)
      Parse the specified source string into a list of strings according to the specified separator.
      Parameters:
      source - the string to parse.
      separator - the delimiter for the resulting strings.
      regex - if true then separator is interpreted as a regex, otherwise it is considered a string litteral.
      Returns:
      a list of strings, possibly empty but never null;
    • parseStringArray

      public static String[] parseStringArray(String source, String separator)
      Parse the specified source string into a list of strings according to the specified separator.
      Parameters:
      source - the string to parse.
      separator - the delimiter for the resulting strings; it can be a regex.
      Returns:
      a list of strings, possibly empty but never null;
    • parseStringArray

      public static String[] parseStringArray(String source, String separator, boolean regex)
      Parse the specified source string into a list of strings according to the specified separator.
      Parameters:
      source - the string to parse.
      separator - the delimiter for the resulting strings; it can be a regex.
      regex - if true then separator is interpreted as a regex, otherwise it is considered a string litteral.
      Returns:
      a list of strings, possibly empty but never null;
    • getDecodedURLPath

      public static String getDecodedURLPath(URL url)
      Transform a file path into a URL in non URL-ncoded form.
      Parameters:
      url - the URL to transform.
      Returns:
      a string representing the decoded URL.
    • unquote

      public static String unquote(String source)
      Remove the leading and trailing quotes form the source string.
      Parameters:
      source - the string to unquote.
      Returns:
      the unquoted string.
    • maskPassword

      public static String maskPassword(String password)
      Mask the specified password.
      Parameters:
      password - the password to mask.
      Returns:
      either the string "null" if password is null, or a sequence of asterisks.