Package org.jppf.utils
Class StringUtils
java.lang.Object
org.jppf.utils.StringUtils
This class provides a set of utility methods for manipulating strings.
- Author:
- Laurent Cohen
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> StringarrayToString(String sep, String prefix, String suffix, T... array) Get a String representation of an array of any type.static <T> StringarrayToString(T... array) Get a String representation of an array of any type.static StringBuild a string made of the specified tokens.static StringbuildString(int[] ports) Convert an array of int values into a space-separated string.static StringbuildString(String sep, String prefix, String suffix, int[] array) Get a String representation of an array of any type.static StringgetDecodedURLPath(URL url) Transform a file path into a URL in non URL-ncoded form.static booleanDetermine whether the specified source string contains one of the specified values.static booleanDetermine whether the specified source string is equal to one of the specified values.static StringmaskPassword(String password) Mask the specified password.static StringFormat 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 StringFormat 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 StringPads 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 StringPads 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 NumberparseNumber(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.parseStrings(String source, String separator) Parse the specified source string into a list of strings according to the specified separator.parseStrings(String source, String separator, boolean regex) Parse the specified source string into a list of strings according to the specified separator.static StringprintClassLoaderHierarchy(ClassLoader leafClassLoader) Print a top-down representation of a class loader hierarchy into a string.static booleanstartsWithOneOf(String source, boolean ignoreCase, String... values) Determine whether the specified source string starts with one of the specified values.static StringtoHexString(byte[] bytes) Convert an array of bytes into a string of hexadecimal numbers.static StringtoHexString(byte[] bytes, int start, int length, String sep) Convert a part of an array of bytes, into a string of hexadecimal numbers.static StringtoIdentityString(Object obj) Return a String in the format <object class name>@hashcode for the specified object.static StringPrint a representation of aURLClassLoaderinto a string.static StringtoStringDuration(long duration) Transform a duration in milliseconds into a string with hours, minutes, seconds and milliseconds..static StringRemove the leading and trailing quotes form the source string.
-
Field Details
-
UTF_8
Charset instance for UTF-8 encoding. -
ZERO_OBJECT
Constant for an empty array of Objects. -
ZERO_URL
Constant for an empty array of URLs.
-
-
Method Details
-
padLeft
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
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- iftrue, then truncate the string if its length is greater thanmaxLen.- Returns:
- a string formatted to the specified length.
-
padRight
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 rightpadChar- the character used for paddingmaxLen- the length to pad the string up to if its length is greater than the padding length- Returns:
- the padded (or truncated) string
-
padRight
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 rightpadChar- the character used for paddingmaxLen- the length to pad the string up to if its length is greater than the padding lengthtruncate- iftrue, then truncate the string if its length is greater thanmaxLen.- Returns:
- the padded (or truncated) string
-
toHexString
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
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 thesepparameter.- 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
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
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
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
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
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
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
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
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
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
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
Print a representation of aURLClassLoaderinto 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
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
defif it could not be parsed as a number.
-
toIdentityString
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
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
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- iftruethenseparatoris interpreted as a regex, otherwise it is considered a string litteral.- Returns:
- a list of strings, possibly empty but never null;
-
parseStringArray
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
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- iftruethenseparatoris interpreted as a regex, otherwise it is considered a string litteral.- Returns:
- a list of strings, possibly empty but never null;
-
getDecodedURLPath
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
Remove the leading and trailing quotes form the source string.- Parameters:
source- the string to unquote.- Returns:
- the unquoted string.
-
maskPassword
Mask the specified password.- Parameters:
password- the password to mask.- Returns:
- either the string "null" if password is null, or a sequence of asterisks.
-