Messageformat.format usage in Java

Source: Internet
Author: User

The Messageformat itself is independent of the locale, and is related to the patterns that the user provides to Messageformat and the sub-format patterns used for the inserted parameters to generate messages for different locales.

Messageformat mode (main section):

formatelement:
{Argumentindex}: is the index of the entry position starting at 0.
{Argumentindex, formattype}
{argumentindex, formattype, Formatstyle}

formattype:: Specifies that the incoming parameter is formatted with a different format subclass. The value range is as follows:
Number: call NumberFormat to format

Date: call dateformat for formatting

Time: call DateFormat to format

Choice: call Choiceformat to format

 formatstyle :: Sets the formatting style used in Formattype. The value range is as follows:
         Short
          Medium
         long
          full
         integer
          currency
         percent
           subformatpattern  (sub-format mode, Shape #.##)

Also in Str, for example, in this string:

1, {0} and {1,number,short} and {2,number,#.#}; formatelement, 0,1,2 is Argumentindex.

2, {1,number,short} inside number belongs to formattype, Short belongs to the Formatstyle.

3, {1,number,#.#} inside the #. #就属于子格式模式.

Specify formattype and Formatstyle to generate values for date formats, numbers with different precision, percent types, and so on.

Instance:

1, Argumentindex must be a non-negative integer, its number is not limited to 0 to 9 of the 10, it can be 0 to 9 of the number, so there can be a good many, such as:

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: Abcdefghi
2. When formatting a string, two single quotes represent a single quotation mark, and a single quotation mark is omitted unless the Chinese single quotation mark is not omitted, such as:

String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh, {0} is ' a ' pig" </span><span style= "font-family:arial, Helvetica, Sans-serif;" "Zhangsan"); </span>system.out.println (value)  ; Output: Oh, Zhangsan is a pig
Add a single quotation mark to the letter A, such as:
String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh, {0} is ' a ' pig" </span><span style= "font-family:arial, Helvetica, Sans-serif;" "Zhangsan"); </span>system.out.println (value)  ; Output: Oh, Zhangsan is ' a ' pig
If you need to display double quotes to be transferred, for example: String msg = "Oh, {0} is \" A\ "pig";

3, single quotation marks will invalidate the placeholder after it, resulting in a direct output placeholder.

Messageformat.format ("{0}{1}",1,2);//ResultsMessageformat.format ("' {0}{1}",1,2);//result {0}{1}Messageformat.format ("' {0} '-{1}",1,2);//Result {0}-2

There is no relationship between using double quotes and two single quotes, such as
String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh, ' {0} ' is a pig" </span><span style= "font-family:arial, Helvetica, Sans-serif;" "Zhangsan"); </span>system.out.println (value)  ; Output: Oh, ' Zhangsan ' is a pig
Another example is the use of a sub-format pattern, with a single quotation mark:
String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh, {0,number,#.#} is good num" </span><span style= "font-family:arial, Helvetica, Sans-serif;" Double.valueof ("3.1415")); </span>system.out.println (value)  ; Output: Oh, 3.1 is good num
3, whether it is a quoted string or an unquoted string, the left curly braces are not supported, such as:

String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh,} is good num" </span><span style= "font-family:arial, Helvetica, Sans-serif;" Double.valueof ("3.1415")); </span>system.out.println (value)  ; Output: Oh,} is good num
If you use the left curly brace, an exception occurs

String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh, {is good num" </span><span style= "font-family:arial, Helvetica, Sans-serif;" Double.valueof ("3.1415")); </span>system.out.println (value)  ; Java.lang.IllegalArgumentException:Unmatched braces in the pattern.

So to use the left curly braces you need to use single quotes with

Messageformat.format ("'{' {0}}""x-rapido"//  {X-rapido}

There is also an interesting phenomenon, if two or more left curly braces appear, there will be a split string, but the right curly brace is no problem, although there is no point, the actual application we also do not use the

String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh, {{is good num" </span><span style= "font-family:arial, Helvetica, Sans-serif;" "D"); </span>system.out.println (value);  
String value = Messageformat.format (<span style= "font-family:arial, Helvetica, Sans-serif;" > "Oh,}} is good num" </span><span style= "font-family:arial, Helvetica, Sans-serif;" "D"); </span>system.out.println (value);  

About the Messageformat.format method:

Each time the Messageformat.format method is called, an instance of the Messageformat is created, equivalent to Messageformat used only once. The format method for the Messageformat class is as follows:

public static string format (string pattern, Object ... arguments)   {      Messageformat temp = new Messageformat (patter n);      return Temp.format (arguments);  }  
therefore, to format a string of the same pattern multiple times, it is better to create a Messageformat instance that performs a formatting operation
String message = "Oh, {0} is a pig";  Messageformat Messageformat = new Messageformat (message);  object[] array = new object[]{"Zhangsan"};  String value = Messageformat.format (array);    



Messageformat.format usage in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.