java.text.MessageFormat
MessageFormat is used to format and parse arguments based on a pattern. The
pattern specifies how each argument will be formatted and concatenated with
other text to produce the formatted output.
Nested Classes
MessageFormat.Field |
The instances of this inner class are used as attribute keys in
AttributedCharacterIterator that
MessageFormat.formatToCharacterIterator() method returns. |
Summary
Public Constructors
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
MessageFormat(String template, Locale locale)
Constructs a new MessageFormat using the specified pattern and the
specified Locale for Formats.
Parameters
template
| the pattern |
locale
| the locale |
public
MessageFormat(String template)
Constructs a new MessageFormat using the specified pattern and the
default Locale for Formats.
Public Methods
public
void
applyPattern(String template)
Changes this MessageFormat to use the specified pattern.
public
Object
clone()
Returns a new instance of MessageFormat with the same pattern and Formats
as this MessageFormat.
Returns
- a shallow copy of this MessageFormat
public
boolean
equals(Object object)
Compares the specified object to this MessageFormat and answer if they
are equal. The object must be an instance of MessageFormat and have the
same pattern.
Parameters
object
| the object to compare with this object |
Returns
- true if the specified object is equal to this MessageFormat,
false otherwise
public
static
String
format(String template, Object[] objects)
Formats the Object arguments using the specified MessageFormat pattern.
Parameters
template
| the pattern |
objects
| the array of Objects to format |
Formats the Object arguments into the specified StringBuffer using the
pattern of this MessageFormat.
If Field Attribute of the FieldPosition supplied is
MessageFormat.Field.ARGUMENT, then begin and end index of this field
position is set to the location of the first occurrence of a message
format argument. Otherwise the FieldPosition is ignored
Parameters
objects
| the array of Objects to format |
buffer
| the StringBuffer |
field
| a FieldPosition. |
Returns
- the StringBuffer parameter
buffer
Formats the specified object into the specified StringBuffer using the
pattern of this MessageFormat.
Parameters
object
| the object to format, must be an array of Object |
buffer
| the StringBuffer |
field
| a FieldPosition which is ignored |
Returns
- the StringBuffer parameter
buffer
Formats the specified object using the rules of this MessageFormat and
returns an AttributedCharacterIterator with the formatted message and
attributes. The AttributedCharacterIterator returned also includes the
attributes from the formats of this MessageFormat.
Parameters
object
| the object to format |
Returns
- an AttributedCharacterIterator with the formatted message and
attributes
public
Format[]
getFormats()
Returns the Formats of this MessageFormat.
public
Format[]
getFormatsByArgumentIndex()
Returns the formats used for each argument index. If an argument is
placed more than once in the pattern string, than returns the format of
the last one.
Returns
- an array of formats, ordered by argument index
public
Locale
getLocale()
Returns the Locale used when creating Formats.
Returns
- the Locale used to create Formats
public
int
hashCode()
Returns an integer hash code for the receiver. Objects which are equal
answer the same value for this method.
Parse the message argument from the specified String starting at the
index specified by the ParsePosition. If the string is successfully
parsed, the index of the ParsePosition is updated to the index following
the parsed text.
Parameters
string
| the String to parse |
position
| the ParsePosition, updated on return with the index following
the parsed text, or on error the index is unchanged and the
error index is set to the index where the error occurred |
Returns
- the array of Object arguments resulting from the parse, or null
if there is an error
Parse the message arguments from the specified String using the rules of
this MessageFormat.
Parameters
string
| the String to parse |
Returns
- the array of Object arguments resulting from the parse
Parse the message argument from the specified String starting at the
index specified by the ParsePosition. If the string is successfully
parsed, the index of the ParsePosition is updated to the index following
the parsed text.
Parameters
string
| the String to parse |
position
| the ParsePosition, updated on return with the index following
the parsed text, or on error the index is unchanged and the
error index is set to the index where the error occurred |
Returns
- the array of Object arguments resulting from the parse, or null
if there is an error
public
void
setFormat(int offset, Format format)
Sets the specified Format used by this MessageFormat.
Parameters
offset
| the format to change |
format
| the Format
|
public
void
setFormatByArgumentIndex(int argIndex, Format format)
Sets the format used for argument at index argIndex
to
format
public
void
setFormats(Format[] formats)
Sets the Formats used by this MessageFormat.
Parameters
formats
| an array of Format
|
public
void
setFormatsByArgumentIndex(Format[] formats)
Sets the formats used for each argument The formats
array
elements should be in the order of the argument indices.
public
void
setLocale(Locale locale)
Sets the Locale to use when creating Formats.
public
String
toPattern()
Returns the pattern of this MessageFormat.