|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.modeshape.common.util.StringUtil
@Immutable public class StringUtil
Utilities for string processing and manipulation.
Field Summary | |
---|---|
static String[] |
EMPTY_STRING_ARRAY
|
Method Summary | |
---|---|
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 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 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 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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String[] EMPTY_STRING_ARRAY
Method Detail |
---|
public static String combineLines(String[] lines)
splitLines(String)
.
lines
- the lines to be combined
public static String combineLines(String[] lines, char separator)
lines
- the lines to be combinedseparator
- the separator character
public static List<String> splitLines(String content)
content
- the string content that is to be split
public static String createString(String pattern, Object... parameters)
{n}
, where n
is an integer starting at 1. Therefore, the first
parameter replaces all occurrences of "{1}", the second parameter replaces all occurrences of "{2}", 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 keys
public 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 0
public 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 enough
justifyLeft(String, int, char)
public 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 needed
public 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 needed
setLength(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 needed
setLength(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 returned
IllegalArgumentException
- 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 used
IllegalArgumentException
- 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 null
IOException
- 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 null
IOException
- 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 written
IOException
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 written
IOException
IllegalArgumentException
- if the writer is nullpublic static String getStackTrace(Throwable throwable)
throwable
- the exception for which the stack trace is to be returned
public static String normalize(String text)
text
- the text to be normalized
public static String getHexString(byte[] bytes)
bytes
- the byte array
public static byte[] fromHexString(String hexadecimal)
public static boolean isBlank(String str)
str
- the string; may be null or empty
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |