java.text.MessageFormat (JDK 1.1)
This class formats and substitutes objects into specified positions in a message string (also known as the "pattern" string). It provides the closest Java equivalent to the printf() function of the C developing language.
If a message is to be displayed only a single time, the simplest way to use the MessageFormat class is through the static format() method which is passed a message or pattern string and an array of argument objects to be formatted and substituted into the string. If the message is to be displayed several times, it makes more sense to create a MessageFormat object, supplying the pattern string, and then to call the format() instance method of this object, supplying the array of objects to be formatted into the message.
The message or pattern string used by the MessageFormat should contain digits enclosed in curly braces to indicate where each argument should be substituted. The sequence "{0}" indicates that the first object should be converted to a string (if necessary) and inserted at that point, while the sequence "{3}" indicates that the fourth object should be inserted, for example. If the object to be inserted is not a string, MessageFormat checks to see if it is a Date or a subclass of Number. If so, it uses a default DateFormat or NumberFormat object to convert the value to a string. If not, it simply invokes the object's toString() method to convert it.
A digit within curly braces in a pattern string may be optionally followed by a comma, and one of the words "date", "time", "number", or "choice", to indicate that the corresponding argument should be formatted as a date, time, number, or choice before being substituted into the pattern string. Any of these keywords may additionally be followed by a comma and additional pattern information to be used in formatting the date, time, number, or choice. (See SimpleDateFormat, DecimalFormat, and ChoiceFormat for more information.)
You can use the setLocale() method to specify a non-default Locale that the MessageFormat should use when obtaining DateFormat and NumberFormat objects to format dates, time, and numbers inserted into the pattern.
You can change the Format object used at a particular position in the pattern with the setFormat() method. You can set a new pattern for the MessageFormat object by calling applyPattern(), and you can obtain a string that represents the current formatting pattern by calling toPattern().
MessageFormat also supports a parse() method that can parse an array of objects out of a specified string, according to the specified pattern.
public classMessageFormatextends Format { //Public ConstructorpublicMessageFormat(Stringpattern); //Class Methodspublic static Stringformat(Stringpattern, Object[]arguments); //Public Instance Methodspublic voidapplyPattern(StringnewPattern); public Objectclone(); //Overrides Formatpublic booleanequals(Objectobj); //Overrides Objectpublic final StringBufferformat(Object[]source, StringBufferresult, FieldPositionignore); public final StringBufferformat(Objectsource, StringBufferresult, FieldPositionignore); //Defines Formatpublic Format[]getFormats(); public LocalegetLocale(); public inthashCode(); //Overrides Objectpublic Object[]parse(Stringsource, ParsePositionstatus); public Object[]parse(Stringsource) throws ParseException; public ObjectparseObject(Stringtext, ParsePositionstatus); //Defines Formatpublic voidsetFormat(intvariable, FormatnewFormat); public voidsetFormats(Format[]newFormats); public voidsetLocale(LocaletheLocale); public StringtoPattern(); }
Hierarchy:
Object->Format(Serializable, Cloneable)->MessageFormat