MessageFormat. format usage in Java, messageformat. format
MessageFormat is independent of the language environment. It is related to the mode provided to MessageFormat by the user and the subformat mode used for inserted parameters to generate messages suitable for different language Environments.
MessageFormat mode (main part ):
FormatElement:
{ArgumentIndex}: Index of the input parameter position starting from 0.
{ArgumentIndex, FormatType}
{ArgumentIndex, FormatType, FormatStyle}
FormatType: Specify to Format input parameters using different Format subclass. The value range is as follows:
Number: Call NumberFormat for formatting
Date: Call DateFormat for formatting
Time: Call DateFormat for formatting
Choice: Call ChoiceFormat for formatting
FormatStyle: Set the format used in FormatType. The value range is as follows:
Short
Medium
Long
Full
Integer
Currency
Percent
SubformatPattern (subformat mode, for example #.##)
Taking str as an example, in this string:
1. Both {0} and {1, number, short} and {2, number, #. #}; belongFormatElement, 0, 1, and 2 are ArgumentIndex.
2. the number in {1, number, short} belongsFormatType, Short belongsFormatStyle.
3. #. # In {1, number, #. #} belongs to the subformat mode.
Specify FormatType and FormatStyle to generate date format values, numbers with different precision, percentage types, and so on.
Instance:
1. ArgumentIndex must be a non-negative integer. Its number is not limited to 10 numbers ranging from 0 to 9. It can be composed of numbers ranging from 0 to 9, so there can be many such:
String msg = "{0} {1} {2} {3} {4} {5} {6} {7} {8 }"; object [] array = new Object [] {"A", "B", "C", "D", "E", "F", "G ", "H", "I",}; String value = MessageFormat. format (msg, array); System. out. println (value); // output: ABCDEFGHI2. When formatting a string, two single quotes indicate one single quotation mark. A single quotation mark is omitted unless the Chinese single quotes are not omitted, for example:
String value = MessageFormat. format ("oh, {0} is 'A' pig", "ZhangSan"); System. out. println (value); // output: oh, ZhangSan is a pigEnclose the letter A with single quotes, for example:
String value = MessageFormat. format ("oh, {0} is ''a' pig", "ZhangSan"); System. out. println (value); // output: oh, ZhangSan is 'A' pigTo transfer a double quotation mark ("), for example, String msg =" oh, {0} is \ "a \" pig ";
3. Single quotes will invalidate all placeholders behind them, resulting in direct output of placeholders.
MessageFormat. format ("{0} {1}", 1, 2); // The result is 12MessageFormat. format ("'{0} {1}", 1, 2); // result {0} {1} MessageFormat. format ("'{0}'-{1}", 1, 2); // result {0}-2
Double quotation marks are irrelevant to the two single quotes. For example
String value = MessageFormat. format ("oh,'' {0} ''is a pig", "ZhangSan"); System. out. println (value); // output: oh, 'hangsan' is a pigFor example, if the subformat mode is used, a single quotation mark is added:
String value = MessageFormat. format ("oh, {0, number ,#. #} is good num ", Double. valueOf ("3.1415"); System. out. println (value); // output: oh, 3.1 is good num3. left curly brackets are not supported, whether there is a quotation mark string or no quotation mark string, for example:
String value = MessageFormat. format ("oh,} is good num", Double. valueOf ("3.1415"); System. out. println (value); // output: oh,} is good numIf left curly braces are used, an exception occurs.
String value = MessageFormat.format("oh, { is good num", Double.valueOf("3.1415"));System.out.println(value); // java.lang.IllegalArgumentException: Unmatched braces in the pattern.
Therefore, to use the left curly braces, you must use single quotes.
MessageFormat.format("'{'{0}}", "X-rapido"); // {X-rapido}
There is another interesting phenomenon. If two or more left curly braces appear, the split string will appear, but the right curly braces are okay, although it makes no sense, we cannot use the actual application either.
String value = MessageFormat.format("oh, {{ is good num", "d");System.out.println(value); // oh,
String value = MessageFormat.format("oh, }} is good num", "d");System.out.println(value); // oh, }} is good num
About the MessageFormat. format method:
Every time the MessageFormat. format method is called, a new MessageFormat instance is created, which is equivalent to MessageFormat only once. The format method of the MessageFormat class is as follows:
public static String format(String pattern, Object ... arguments) { MessageFormat temp = new MessageFormat(pattern); return temp.format(arguments); } Therefore, to format strings in the same format multiple times, it is better to create a MessageFormat instance before formatting.
String message = "oh, {0} is a pig"; MessageFormat messageFormat = new MessageFormat(message); Object[] array = new Object[]{"ZhangSan"}; String value = messageFormat.format(array); System.out.println(value);