public class StringFormatHelper extends Object
Objects of this class contain no data. As such, this class is implemented as a Singleton.
Modifier and Type | Method and Description |
---|---|
protected String[] |
breakString(String s,
int maxLen)
Breaks a String along word boundarys to the specified maximum length.
|
String |
formatBlockedText(String s,
int indent,
int lineLen)
Splits the specified String into lines that are indented by the specified indent and are of length less than or
equal to the specified line length.
|
String |
formatHangingIndent(String s,
int indent,
int lineLen)
Formats a string with a hanging indent.
|
String |
formatLabeledList(String[] labels,
String[] texts,
String divider,
int maxIndent,
int lineLen)
Formats a "labeled list" (like a bullet or numbered list, only with labels for each item).
|
static StringFormatHelper |
getHelper()
Gets the one and only instance of the StringFormatHelper
|
public static StringFormatHelper getHelper()
public String formatBlockedText(String s, int indent, int lineLen)
If s
is null or empty, an empty String is returned.
s
- the String to be formattedindent
- the length of the indent for the text blocklineLen
- the maximum line length for the text block, including the indentIllegalArgumentException
- if lineLen
is less than indent
or if lineLen
is less than 0.public String formatHangingIndent(String s, int indent, int lineLen)
The returned String is formated such that:
lineLen
indent
spaces
indent
- the number of spaces to indent all but the first line (may be 0)lineLen
- the maximum line lengths
- the string to be formattedIllegalArgumentException
- if lineLen
is less than indent
or if lineLen
is less than 0.public String formatLabeledList(String[] labels, String[] texts, String divider, int maxIndent, int lineLen)
Example:
System.out .println(formatLabeledList(new String[] { "old_file", "new_file" }, new String[] { "the name of the file to copy - this file " + "must already exist, be readable, and " + "end with '.html'", "the name of the file to receive the copy" }, " = ", 20, 80));produces....
old_file = the name of the file to copy - this file must already exist, be readable, and end with '.html' new_file = the name of the file to copy to
labels
- An array of labels.texts
- An array of texts to go with the labels.divider
- The divider to go between the labels and texts. This will be right-aligned against the texts.maxIndent
- Specifies the maximum indent for the text to be written out. If the combination of a label and divider
is longer than maxIndent, the text will be written out in a block starting on the line following the
label and divider, rather than on the same line.lineLen
- The maximum length of returned lines.IllegalArgumentException
- if labels
and text
do not have the same number of elements.protected String[] breakString(String s, int maxLen)
This method returns an array of two strings:
The first String is a line, of length less than maxLen
. If this String is not the entire passed
String, it will be terminated with a newline.
The second String is the remainder of the original String. If the original String had been broken on a space (as opposed to a newline that had been in the original String) all leading spaces will have been removed. If there is no remainder, null is returned as the second String and no newline will have been appended to the first String.
s
- The String to be broken. If null, will be converted to an empty string.maxLen
- the maximum line length of the first returned stringCopyright © 2017. All rights reserved.