Package com.ibm.icu.text
Class SimpleFormatter
java.lang.Object
com.ibm.icu.text.SimpleFormatter
Formats simple patterns like "{1} was born in {0}".
Minimal subset of MessageFormat; fast, simple, minimal dependencies.
Supports only numbered arguments with no type nor style parameters,
and formats only string values.
Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
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"));
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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.format
(CharSequence... values) Formats the given values.formatAndAppend
(StringBuilder appendTo, int[] offsets, CharSequence... values) Formats the given values, appending to the appendTo builder.formatAndReplace
(StringBuilder result, int[] offsets, CharSequence... values) Formats the given values, replacing the contents of the result builder.int
Returns the pattern text with none of the arguments.toString()
Returns a string similar to the original pattern, only for debugging.
-
Method Details
-
compile
Creates a formatter from the pattern string.- Parameters:
pattern
- The pattern string.- Returns:
- The new SimpleFormatter object.
- Throws:
IllegalArgumentException
- for bad argument syntax.
-
compileMinMaxArguments
Creates a formatter from the pattern string. The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.- Parameters:
pattern
- The pattern string.min
- The pattern must have at least this many arguments.max
- The pattern must have at most this many arguments.- Returns:
- The new SimpleFormatter object.
- Throws:
IllegalArgumentException
- for bad argument syntax and too few or too many arguments.
-
getArgumentLimit
public int getArgumentLimit()- Returns:
- The max argument number + 1.
-
format
Formats the given values. -
formatAndAppend
Formats the given values, appending to the appendTo builder.- Parameters:
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.- Returns:
- appendTo
-
formatAndReplace
Formats the given values, replacing the contents of the result builder. May optimize by actually appending to the result if it is the same object as the value corresponding to the initial argument in the pattern.- Parameters:
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().- Returns:
- result
-
toString
Returns a string similar to the original pattern, only for debugging. -
getTextWithNoArguments
Returns the pattern text with none of the arguments. Like formatting with all-empty string values.
-