public final class SimpleFormatter extends Object
Factory methods throw exceptions for syntax errors and for too few or too many arguments/placeholders.
SimpleFormatter objects are immutable and can be safely cached like strings.
Example:
SimpleFormatter fmt = SimpleFormatter.compile("{1} '{born}' in {0}"); // Output: "paul {born} in england" System.out.println(fmt.format("england", "paul"));
MessageFormat
,
MessagePattern.ApostropheMode
Modifier and Type | Method and Description |
---|---|
static SimpleFormatter |
compile(CharSequence pattern)
Creates a formatter from the pattern string.
|
static SimpleFormatter |
compileMinMaxArguments(CharSequence pattern,
int min,
int max)
Creates a formatter from the pattern string.
|
String |
format(CharSequence... values)
Formats the given values.
|
StringBuilder |
formatAndAppend(StringBuilder appendTo,
int[] offsets,
CharSequence... values)
Formats the given values, appending to the appendTo builder.
|
StringBuilder |
formatAndReplace(StringBuilder result,
int[] offsets,
CharSequence... values)
Formats the given values, replacing the contents of the result builder.
|
int |
getArgumentLimit() |
String |
getTextWithNoArguments()
Returns the pattern text with none of the arguments.
|
String |
toString()
Returns a string similar to the original pattern, only for debugging.
|
public static SimpleFormatter compile(CharSequence pattern)
pattern
- The pattern string.IllegalArgumentException
- for bad argument syntax.public static SimpleFormatter compileMinMaxArguments(CharSequence pattern, int min, int max)
pattern
- The pattern string.min
- The pattern must have at least this many arguments.max
- The pattern must have at most this many arguments.IllegalArgumentException
- for bad argument syntax and too few or too many arguments.public int getArgumentLimit()
public String format(CharSequence... values)
public StringBuilder formatAndAppend(StringBuilder appendTo, int[] offsets, CharSequence... values)
appendTo
- Gets the formatted pattern and values appended.offsets
- offsets[i] receives the offset of where
values[i] replaced pattern argument {i}.
Can be null, or can be shorter or longer than values.
If there is no {i} in the pattern, then offsets[i] is set to -1.values
- The argument values.
An argument value must not be the same object as appendTo.
values.length must be at least getArgumentLimit().
Can be null if getArgumentLimit()==0.public StringBuilder formatAndReplace(StringBuilder result, int[] offsets, CharSequence... values)
result
- Gets its contents replaced by the formatted pattern and values.offsets
- offsets[i] receives the offset of where
values[i] replaced pattern argument {i}.
Can be null, or can be shorter or longer than values.
If there is no {i} in the pattern, then offsets[i] is set to -1.values
- The argument values.
An argument value may be the same object as result.
values.length must be at least getArgumentLimit().public String toString()
public String getTextWithNoArguments()
Copyright © 2016 Unicode, Inc. and others.