public final class StringUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
StringUtils.Align
Enumeration for alignment.
|
Modifier and Type | Field and Description |
---|---|
static String |
COMMA
The comma (",") character as a string.
|
static String |
DEFAULT_JOIN_DELIMITER
Default delimiter for the
join(java.util.Collection<?>) methods. |
static String |
EMPTY
The empty string.
|
static String |
EOL
The platform dependent line separator (new line character).
|
static String |
TAB
The tab character.
|
static String |
UNDERSCORE
The underscore character.
|
static String |
UTF8
String representing UTF-8 encoding (e.g.
|
static String |
ZERO
The zero ("0") character as a string.
|
Modifier and Type | Method and Description |
---|---|
static String |
columnize(String[] strings)
Formats the supplied strings so that they are arranged in
neat columns; maximum line width is 78; space between columns is 2;
no indent.
|
static String |
columnize(String[] strings,
int maxWidth)
Formats the supplied strings so that they are arranged in
neat columns; space between columns is 2; no indent.
|
static String |
columnize(String[] strings,
int maxWidth,
int colSpacing)
Formats the supplied strings so that they are arranged in
neat columns, but with no indent.
|
static String |
columnize(String[] strings,
int maxWidth,
int colSpacing,
int indent)
Formats the supplied strings so that they are arranged in neat columns.
|
static String |
commaSeparated(Collection<?> items)
Returns a comma-separated string of the items in the collection.
|
static String |
concat(Object... values)
Concatenates an array of objects using a StringBuilder and its
append() method. |
static String |
emptyIsNull(String s)
Convert the given string to null, if it is already null or is empty.
|
static boolean |
equals(String one,
String two)
Determine if two Strings, considering nulls to be identical, are equal.
|
static String |
format(String fmt,
Object... o)
A simple string formatter that replaces each occurrence of
{} in the format string with the string representation
of each of the subsequent, optional arguments. |
static String |
fromCamelCase(String cc)
This method expects an input string consisting of letters and digits
in "camelCase" (the standard format for variable names), and converts
it to uppercase letters with underscore delimiters between the words.
|
static String |
getFileContents(String path,
ClassLoader classLoader)
Read in the contents of a text file from the given path.
|
static boolean |
isAlphaNumeric(String value)
This predicate returns true if the specified string is alpha numeric;
that is to say, if it begins with a letter and consists only of letters,
numbers and underscores.
|
static boolean |
isAlphaNumeric(String value,
boolean allowUnderscores)
This predicate returns true if the specified string is alpha numeric;
that is to say, if it begins with a letter and consists only of letters
and numbers.
|
static boolean |
isEmpty(String value)
This predicate returns true if the specified parameter is null, or
consists only of whitespace.
|
static boolean |
isIntegerZeroTo255(String value)
This predicate returns true if the specified string, when converted
to an int, lies in the range 0 - 255.
|
static boolean |
isStandardIdentifier(String id)
This predicate returns true if the supplied string matches the regular
expression defined for general identifiers.
|
static String |
join(Collection<?> values)
Joins a collection of objects' toString() values into a single string,
using the default delimiter of ", " (comma-space).
|
static String |
join(Collection<?> values,
String delim)
Joins a collection of objects' toString() values into a single string,
using the given delimiter.
|
static String |
join(Object[] values)
Joins an array of objects' toString() values into a single string,
using the default delimiter of ", " (comma-space).
|
static String |
join(Object[] values,
String delim)
Joins an array of objects' toString() values into a single string,
using the given delimiter.
|
static String |
normalizeEOL(String string)
Normalizes any compound line breaks (\r\n or \n\r) to only \n.
|
static String |
nullIsEmpty(String s)
Convert the given string to an empty string, if it is null.
|
static String |
pad(String what,
int width)
Pads a field to the specified field width.
|
static String |
pad(String what,
int width,
char with)
Pads a field to the specified field width.
|
static String |
pad(String what,
int width,
char with,
StringUtils.Align align)
Pads a field to the specified field width.
|
static String |
pad(String what,
int width,
char with,
StringUtils.Align align,
boolean noTruncate)
Pads a field to the specified field width.
|
static String |
pad(String what,
int width,
StringUtils.Align align)
Pads a field to the specified field width.
|
static int |
parseHex(String hex,
int fallback)
Returns either the correctly parsed hex value (as an int) or the
supplied fallback value.
|
static int |
parseInt(String value,
int fallback)
Returns either the correctly parsed int value or the supplied
fallback value.
|
static String |
quoted(String value)
Returns the given value wrapped in double-quote characters (") if the
value is not null, or the string
"null" if it is null. |
static String |
safeToString(Object o)
Safely invokes
toString on the specified entity and returns the
result, unless the specified object is null, in which case it will
return null. |
static String |
spaces(int length)
Returns a string of spaces of the specified length.
|
static String |
stripCommentLines(String s)
Removes all lines that begin with the '#' character.
|
static String |
toCamelCase(Enum<?> e)
A convenience overloaded method for
toCamelCase(String)
which takes enumeration constants as the parameter. |
static String |
toCamelCase(String s)
This method expects an input string consisting of uppercase letters,
digits, and underscores (the standard format for constant names), and
converts it to "camelCase".
|
static String |
toCamelCase(String prefix,
Enum<?> e)
A convenience overloaded method for
toCamelCase(String, String) , which uses e.name() . |
static String |
toCamelCase(String prefix,
String s)
This method expects a string consisting of uppercase letters, digits,
and underscores (the standard format for constant names), and converts
it to "camelCase".
|
static String |
trimmedEmptyIsNull(String s)
Convert the given string to null, if it is already null or is empty if
trimmed.
|
static String |
trimPunct(String value)
Removes any punctuation in the passed in string.
|
static String |
trimPunctAndSpaceWith(String value,
String replace)
Removes any punctuation and whitespace in the passed in string.
|
static void |
validateStandardIdentifier(String id)
This method checks the supplied string to see that it matches the
regular expression defined for general identifiers.
|
static String |
zeroFill(int value,
int fieldWidth)
Returns a zero-filled string.
|
public static final String EOL
public static final String TAB
public static final String EMPTY
public static final String UNDERSCORE
public static final String ZERO
public static final String COMMA
public static final String UTF8
public static final String DEFAULT_JOIN_DELIMITER
join(java.util.Collection<?>)
methods.public static boolean isEmpty(String value)
value
- the string value to testpublic static String safeToString(Object o)
toString
on the specified entity and returns the
result, unless the specified object is null, in which case it will
return null.o
- object on which to invoke toStringo.toString()
invocation or null if the object
is nullpublic static boolean isAlphaNumeric(String value, boolean allowUnderscores)
value
- the string value to testallowUnderscores
- if true, underscores are allowedNullPointerException
- if value is nullpublic static boolean isAlphaNumeric(String value)
value
- the string value to testNullPointerException
- if value is nullpublic static String quoted(String value)
"null"
if it is null.value
- the valuepublic static String toCamelCase(Enum<?> e)
toCamelCase(String)
which takes enumeration constants as the parameter.e
- the enum constantpublic static String toCamelCase(String s)
For example, "LORD_OF_THE_RINGS"
becomes
"lordOfTheRings"
.
s
- the uppercase words delimited by underscores input stringNullPointerException
- if the parameter is nullIllegalArgumentException
- if s has unexpected (or no) characters
in itpublic static String toCamelCase(String prefix, Enum<?> e)
toCamelCase(String, String)
, which uses e.name()
.prefix
- optional prefixe
- the enum constantpublic static String toCamelCase(String prefix, String s)
For example:
"LORD_OF_THE_RINGS" --> "lordOfTheRings" "LORD_OF_THE_RINGS" and prefix "prequelTo" --> "prequelToLordOfTheRings"
prefix
- optional prefixs
- the uppercase words delimited by underscores input stringNullPointerException
- if the parameter is nullIllegalArgumentException
- if s has unexpected (or no) characters
in itpublic static String fromCamelCase(String cc)
For example, "lordOfTheRings"
becomes
"LORD_OF_THE_RINGS"
.
cc
- the camelCase input stringNullPointerException
- if the parameter is nullIllegalArgumentException
- if s has unexpected (or no) characters
in itpublic static boolean isIntegerZeroTo255(String value)
value
- the string value to testNullPointerException
- if value is nullpublic static boolean isStandardIdentifier(String id)
id
- the identifierpublic static void validateStandardIdentifier(String id)
id
- the identifierIllegalArgumentException
- if the string does not match the
pattern for standard identifiers.public static boolean equals(String one, String two)
one
- the first String to comparetwo
- the second String to comparepublic static int parseHex(String hex, int fallback)
hex
- String to parse as a hex valuefallback
- The int to return if the supplied hex string is invalidpublic static int parseInt(String value, int fallback)
value
- String to parse as an intfallback
- The int to return if the supplied string is invalidpublic static String zeroFill(int value, int fieldWidth)
StringUtils.zeroFill(45, 6);returns the string
"000045"
.value
- the valuefieldWidth
- the field widthpublic static String format(String fmt, Object... o)
{}
in the format string with the string representation
of each of the subsequent, optional arguments.
For example:
StringUtils.format("{} => {}", "Foo", 123); // returns "Foo => 123"
fmt
- the format stringo
- arguments to be inserted into the output stringpublic static String columnize(String[] strings, int maxWidth, int colSpacing, int indent)
strings
- the array of strings to formatmaxWidth
- the maximum number of characters any single line can becolSpacing
- the number of spaces between columnsindent
- number of spaces to indent the whole thing bypublic static String columnize(String[] strings, int maxWidth, int colSpacing)
strings
- the array of strings to formatmaxWidth
- the maximum number of characters any single line can becolSpacing
- the number of spaces between columnspublic static String columnize(String[] strings, int maxWidth)
strings
- the array of strings to formatmaxWidth
- the maximum number of characters any single line can bepublic static String columnize(String[] strings)
strings
- the array of strings to formatpublic static String pad(String what, int width, char with, StringUtils.Align align, boolean noTruncate)
what
- the string to be paddedwidth
- the width of the padded (truncated) fieldwith
- the character to pad withalign
- the alignmentnoTruncate
- do not truncate the string if it is longer than widthpublic static String pad(String what, int width, char with, StringUtils.Align align)
what
- the string to be paddedwidth
- the width of the padded (truncated) fieldwith
- the character to pad withalign
- the alignmentpublic static String pad(String what, int width, char with)
what
- the string to be paddedwidth
- the width of the padded (truncated) fieldwith
- the character to pad withpublic static String pad(String what, int width, StringUtils.Align align)
what
- the string to be paddedwidth
- the width of the padded (truncated) fieldalign
- one of LEFT, CENTER or RIGHTpublic static String pad(String what, int width)
what
- the string to be paddedwidth
- the width of the padded (truncated) fieldpublic static String spaces(int length)
length
- the lengthpublic static String commaSeparated(Collection<?> items)
Object#toString()
on each item in the collection.items
- collection of itemspublic static String emptyIsNull(String s)
s
- string to be verifiedpublic static String trimmedEmptyIsNull(String s)
s
- string to be verifiedpublic static String nullIsEmpty(String s)
s
- string to be verifiedpublic static String trimPunct(String value)
value
- the original string value that may contain punctuationNullPointerException
- if value is nullpublic static String trimPunctAndSpaceWith(String value, String replace)
value
- the original string value that may contain punctuationreplace
- string to be replaced instead of punctuation and
white spaceNullPointerException
- if value is nullpublic static String join(Collection<?> values)
values
- the valuespublic static String join(Object[] values)
values
- the valuespublic static String join(Collection<?> values, String delim)
values
- the valuesdelim
- the delimiterpublic static String join(Object[] values, String delim)
values
- the valuesdelim
- the delimiterpublic static String concat(Object... values)
append()
method. This convenience method reduces something like:
String s = new StringBuilder().append(e1).append(e2).append(e3).toString();to
String s = StringUtils.concat(e1, e2, e3);
values
- the values to concatenatepublic static String normalizeEOL(String string)
string
- string to be normalizedpublic static String stripCommentLines(String s)
s
- the multiline inputpublic static String getFileContents(String path, ClassLoader classLoader) throws IOException
path
- path of resource containing the dataclassLoader
- class-loader to be used for locating resourceIOException
- if there is a problem reading the file.Copyright © 2015. All Rights Reserved.