Top Description Fields Constructors Methods
com.ibm.icu.text

public final Class SimpleFormatter

extends Object
Class Inheritance
Imports
com.ibm.icu.impl.SimpleFormatterImpl

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"));
ICU Status
Stable since ICU 57.
See Also
MessageFormat, MessagePattern.ApostropheMode

Field Summary

Modifier and TypeField and Description
private final String
compiledPattern

Binary representation of the compiled pattern.

Constructor Summary

AccessConstructor and Description
private
SimpleFormatter(String compiledPattern)

Method Summary

Modifier and TypeMethod and Description
public static SimpleFormatter

Returns:

The new SimpleFormatter object.
compile
(CharSequence
The pattern string.
pattern
)

Creates a formatter from the pattern string.

public static SimpleFormatter

Returns:

The new SimpleFormatter object.
compileMinMaxArguments
(CharSequence
The pattern string.
pattern
,
int
The pattern must have at least this many arguments.
min
,
int
The pattern must have at most this many arguments.
max
)

Creates a formatter from the pattern string.

public String
format(CharSequence... values)

Formats the given values.

public StringBuilder

Returns:

appendTo
formatAndAppend
(StringBuilder
Gets the formatted pattern and values appended.
appendTo
,
int[]
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.
offsets
,
CharSequence...
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.
values
)

Formats the given values, appending to the appendTo builder.

public StringBuilder

Returns:

result
formatAndReplace
(StringBuilder
Gets its contents replaced by the formatted pattern and values.
result
,
int[]
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.
offsets
,
CharSequence...
The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().
values
)

Formats the given values, replacing the contents of the result builder.

public int

Returns:

The max argument number + 1.
getArgumentLimit
()

public String
getTextWithNoArguments()

Returns the pattern text with none of the arguments.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string similar to the original pattern, only for debugging.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

compiledPatternback to summary
private final String compiledPattern

Binary representation of the compiled pattern.

See Also
SimpleFormatterImpl

Constructor Detail

SimpleFormatterback to summary
private SimpleFormatter(String compiledPattern)

Method Detail

compileback to summary
public static SimpleFormatter compile(CharSequence pattern)

Creates a formatter from the pattern string.

Parameters
pattern:CharSequence

The pattern string.

Returns:SimpleFormatter

The new SimpleFormatter object.

Exceptions
IllegalArgumentException:
for bad argument syntax.
ICU Status
Stable since ICU 57.
compileMinMaxArgumentsback to summary
public static SimpleFormatter compileMinMaxArguments(CharSequence pattern, int min, int max)

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:CharSequence

The pattern string.

min:int

The pattern must have at least this many arguments.

max:int

The pattern must have at most this many arguments.

Returns:SimpleFormatter

The new SimpleFormatter object.

Exceptions
IllegalArgumentException:
for bad argument syntax and too few or too many arguments.
ICU Status
Stable since ICU 57.
formatback to summary
public String format(CharSequence... values)

Formats the given values.

ICU Status
Stable since ICU 57.
formatAndAppendback to summary
public StringBuilder formatAndAppend(StringBuilder appendTo, int[] offsets, CharSequence... values)

Formats the given values, appending to the appendTo builder.

Parameters
appendTo:StringBuilder

Gets the formatted pattern and values appended.

offsets:int[]

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:CharSequence[]

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:StringBuilder

appendTo

ICU Status
Stable since ICU 57.
formatAndReplaceback to summary
public StringBuilder formatAndReplace(StringBuilder result, int[] offsets, CharSequence... values)

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:StringBuilder

Gets its contents replaced by the formatted pattern and values.

offsets:int[]

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:CharSequence[]

The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().

Returns:StringBuilder

result

ICU Status
Stable since ICU 57.
getArgumentLimitback to summary
public int getArgumentLimit()
Returns:int

The max argument number + 1.

ICU Status
Stable since ICU 57.
getTextWithNoArgumentsback to summary
public String getTextWithNoArguments()

Returns the pattern text with none of the arguments. Like formatting with all-empty string values.

ICU Status
Stable since ICU 57.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string similar to the original pattern, only for debugging.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
ICU Status
Stable since ICU 57.