|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.text.StringJustifier
A StringJustifier
formats strings according to criteria selected by the user. The user can control various formatting parameters, such as field width, justification style (left-align, right-align, center, etc.), padding characters, etc.
Since applications which must format text frequently format more than one piece of text in the same style, this class is designed to "remember" the selected formatting parameters so that the settings can be reused with each format call. The reduces the amount of parameters which must be passed and results in more efficient execution.
Example: Output the contents of string array
names
, one element per line, right-justified in a 50-character-wide field. For strings shorter than 50 characters, fill in the open space on the left with an underscore character.String[] names = {"apple", "banana", "cherry"}; StringJustifier justifier = new StringJustifier (); justifier . field_width = 50; justifier . cropping = JUSTIFY.RIGHT; justifier . pad_char = '_'; for (int i = 0; i < names . length; ++ i) System.out . println (justifier . format (names [i])); The output of this code is:
_____________________________________________apple ____________________________________________banana ____________________________________________cherry
The formatting parameters can be updated at any time, and doing so will change the way text is formatted afterwards. For a detailed description of user-controllable parameters, see the documentation for each parameter below.
Field Summary | |
int |
cropping
Direction of cropping. |
int |
field_width
Width of the field in which the text should be justified. |
int |
justification
Direction of justification. |
char |
pad_char
Character to use for field padding. |
Constructor Summary | |
StringJustifier()
Initializes a new StringJustifier with default settings. |
Method Summary | |
java.lang.String |
format(java.lang.String string)
Formats a string according to currently selected criteria. |
java.lang.String |
format(java.lang.String prefix,
java.lang.String suffix)
Formats a split string according to selected criteria. |
java.lang.String |
toString()
Generates a string representation this StringJustifier . |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Field Detail |
public int field_width
public int justification
JUSTIFY
constants. Default value: JUSTIFY.RIGHT
.public char pad_char
public int cropping
CROP
constants. (Default value: CROP.NONE
.)Constructor Detail |
public StringJustifier()
StringJustifier
with default settings.Method Detail |
public final java.lang.String format(java.lang.String string)
justification
is set to Justification.FULL
, then the split-string format method (format(String,String)) must be used (for now).string
- the string to justifypublic final java.lang.String format(java.lang.String prefix, java.lang.String suffix)
justification
is set to Justification.FULL
, then the prefix is left-aligned and the suffix is right-aligned. Otherwise, the strings are concatenated together and the resulting string is justified according to selected criteria.prefix
- the left portion of the stringsuffix
- the right portion of the stringpublic java.lang.String toString()
StringJustifier
. Used primarily for debugging.
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |