MessageFormat parseObject Example
MessageFormat class parseObject example. This example shows you how to use parseObject method.
MessageFormat class parseObject example. public Object parseObject(String source, ParsePosition pos) Parses text from a string to produce an object array. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string
Here is the code
/*
* @ # ParseObject.java
* A class representing use to parseObject method
* of MessageFormat class in java.text package
* version 19 June 2008
* author Rose India
*/
import java.text.*;
public class ParseObject {
public static void main(String[] args) {
// Create a pattern for our MessageFormat object to use.
String pattern = "I bought {0,number,#} " +
"apples for {1,number,currency} " +
"on {2,date,dd-MMM-yyyy}.";
// Create a MessageFormat object and apply the pattern
// to it.
MessageFormat mFmt = new MessageFormat(pattern);
Object[] values = {mFmt.parseObject("I bought 20 apples for" +
" $14.18 on 01-Nov-2007.", new ParsePosition(10))
};
// Print out the pattern being used for formatting
// and the formatted output.
System.out.println(mFmt.format(values));
}
} |
Output
| I bought null apples for {1} on {2}. |
|