@Immutable public class StringUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
StringUtil.Justify |
Modifier and Type | Field and Description |
---|---|
static String[] |
EMPTY_STRING_ARRAY |
Modifier and Type | Method and Description |
---|---|
static String |
combineLines(String[] lines)
Combine the lines into a single string, using the new line character as the delimiter.
|
static String |
combineLines(String[] lines,
char separator)
Combine the lines into a single string, using the supplied separator as the delimiter.
|
static boolean |
containsAnyOf(String str,
char... chars)
Return whether the supplied string contains any of the supplied characters.
|
static String |
createString(char charToRepeat,
int numberOfRepeats)
Create a new string containing the specified character repeated a specific number of times.
|
static String |
createString(String pattern,
Object... parameters)
Create a string by substituting the parameters into all key occurrences in the supplied format.
|
static byte[] |
fromHexString(String hexadecimal) |
static String |
getHexString(byte[] bytes)
Get the hexadecimal string representation of the supplied byte array.
|
static String |
getStackTrace(Throwable throwable)
Get the stack trace of the supplied exception.
|
static boolean |
isBlank(String str)
Returns true if the given string is null or represents the empty string
|
static boolean |
isHexCharacter(char c) |
static boolean |
isHexString(String hexadecimal) |
static String |
join(Iterable<?> values,
String delimiter)
Combine the supplied values into a single string, using the supplied string to delimit values.
|
static String |
join(Object[] values,
String delimiter)
Combine the supplied values into a single string, using the supplied string to delimit values.
|
static String |
justify(StringUtil.Justify justify,
String str,
int width,
char padWithChar)
Justify the contents of the string.
|
static String |
justifyCenter(String str,
int width,
char padWithChar)
Center the contents of the string.
|
static String |
justifyLeft(String str,
int width,
char padWithChar)
Left justify the contents of the string, ensuring that the supplied string begins at the first character and that the
resulting string is of the desired length.
|
protected static String |
justifyLeft(String str,
int width,
char padWithChar,
boolean trimWhitespace) |
static String |
justifyRight(String str,
int width,
char padWithChar)
Right justify the contents of the string, ensuring that the string ends at the last character.
|
static String |
normalize(String text)
Removes leading and trailing whitespace from the supplied text, and reduces other consecutive whitespace characters to a
single space.
|
static boolean |
notBlank(String str)
Returns true if the given string is not null and does not represents the empty string
|
static String |
read(InputStream stream)
Read and return the entire contents of the supplied
InputStream . |
static String |
read(Reader reader)
Read and return the entire contents of the supplied
Reader . |
static String |
setLength(String original,
int length,
char padChar)
Set the length of the string, padding with the supplied character if the supplied string is shorter than desired, or
truncating the string if it is longer than desired.
|
static List<String> |
splitLines(String content)
Split the supplied content into lines, returning each line as an element in the returned list.
|
static String |
truncate(Object obj,
int maxLength)
Truncate the supplied string to be no more than the specified length.
|
static String |
truncate(Object obj,
int maxLength,
String suffix)
Truncate the supplied string to be no more than the specified length.
|
static void |
write(String content,
OutputStream stream)
Write the entire contents of the supplied string to the given stream.
|
static void |
write(String content,
Writer writer)
Write the entire contents of the supplied string to the given writer.
|
public static final String[] EMPTY_STRING_ARRAY
public static String combineLines(String[] lines)
splitLines(String)
.lines
- the lines to be combinedpublic static String combineLines(String[] lines, char separator)
lines
- the lines to be combinedseparator
- the separator characterpublic static List<String> splitLines(String content)
content
- the string content that is to be splitpublic static String join(Object[] values, String delimiter)
values
- the values to be combined; may not be null, but may contain null values (which are skipped)delimiter
- the characters to place between each of the values; may not be nullString.split(String)
public static String join(Iterable<?> values, String delimiter)
values
- the values to be combined; may not be null, but may contain null values (which are skipped)delimiter
- the characters to place between each of the values; may not be nullString.split(String)
public static String createString(String pattern, Object... parameters)
{n}
, where n
is an integer starting at 0. Therefore, the first
parameter replaces all occurrences of "{0}", the second parameter replaces all occurrences of "{1}", etc.
If any parameter is null, the corresponding key is replaced with the string "null". Therefore, consider using an empty string when keys are to be removed altogether.
If there are no parameters, this method does nothing and returns the supplied pattern as is.
pattern
- the patternparameters
- the parameters used to replace keyspublic static String createString(char charToRepeat, int numberOfRepeats)
charToRepeat
- the character to repeatnumberOfRepeats
- the number of times the character is to repeat in the result; must be greater than 0public static String setLength(String original, int length, char padChar)
justifyLeft(String, int, char)
, this method does not
remove leading and trailing whitespace.original
- the string for which the length is to be set; may not be nulllength
- the desired length; must be positivepadChar
- the character to use for padding, if the supplied string is not long enoughjustifyLeft(String, int, char)
public static String justify(StringUtil.Justify justify, String str, int width, char padWithChar)
justify
- the way in which the string is to be justifiedstr
- the string to be right justified; if null, an empty string is usedwidth
- the desired width of the string; must be positivepadWithChar
- the character to use for padding, if neededpublic static String justifyRight(String str, int width, char padWithChar)
str
- the string to be right justified; if null, an empty string is usedwidth
- the desired width of the string; must be positivepadWithChar
- the character to use for padding, if neededpublic static String justifyLeft(String str, int width, char padWithChar)
str
- the string to be left justified; if null, an empty string is usedwidth
- the desired width of the string; must be positivepadWithChar
- the character to use for padding, if neededsetLength(String, int, char)
protected static String justifyLeft(String str, int width, char padWithChar, boolean trimWhitespace)
public static String justifyCenter(String str, int width, char padWithChar)
str
- the string to be left justified; if null, an empty string is usedwidth
- the desired width of the string; must be positivepadWithChar
- the character to use for padding, if neededsetLength(String, int, char)
public static String truncate(Object obj, int maxLength)
obj
- the object from which the string is to be obtained using Object.toString()
.maxLength
- the maximum length of the string being returnedIllegalArgumentException
- if the maximum length is negativepublic static String truncate(Object obj, int maxLength, String suffix)
obj
- the object from which the string is to be obtained using Object.toString()
.maxLength
- the maximum length of the string being returnedsuffix
- the suffix that should be added to the content if the string must be truncated, or null if the default suffix
of "..." should be usedIllegalArgumentException
- if the maximum length is negativepublic static String read(Reader reader) throws IOException
Reader
. This method always closes the reader when finished
reading.reader
- the reader of the contents; may be nullIOException
- if there is an error reading the contentpublic static String read(InputStream stream) throws IOException
InputStream
. This method always closes the stream when finished
reading.stream
- the streamed contents; may be nullIOException
- if there is an error reading the contentpublic static void write(String content, OutputStream stream) throws IOException
content
- the content to write to the stream; may be nullstream
- the stream to which the content is to be writtenIOException
IllegalArgumentException
- if the stream is nullpublic static void write(String content, Writer writer) throws IOException
content
- the content to write to the writer; may be nullwriter
- the writer to which the content is to be writtenIOException
IllegalArgumentException
- if the writer is nullpublic static String getStackTrace(Throwable throwable)
throwable
- the exception for which the stack trace is to be returnedpublic static String normalize(String text)
text
- the text to be normalizedpublic static String getHexString(byte[] bytes)
bytes
- the byte arraypublic static byte[] fromHexString(String hexadecimal)
public static boolean isHexString(String hexadecimal)
public static boolean isHexCharacter(char c)
public static boolean isBlank(String str)
str
- the string; may be null or emptypublic static boolean notBlank(String str)
str
- the string; may be null or emptypublic static boolean containsAnyOf(String str, char... chars)
str
- the string to be examined; may not be nullchars
- the characters to be found within the supplied string; may be zero-lengthCopyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.