Examples of Messageformat.format usages in Java

Source: Internet
Author: User
Tags dateformat range string format

This article mainly introduces the Messageformat.format usages in Java, this article first explained the syntax of Messageformat, and then gave a number of operational examples, the need for friends can refer to the

The messageformat itself is not related to the locale, but to the schema provided to Messageformat by the user and to the child format pattern used for the inserted parameters to generate messages that apply to different locales.

Messageformat mode (main part):

The code is as follows:

Formatelement:

{Argumentindex}: is an index of the entry location starting from 0.

{Argumentindex, formattype}

{argumentindex, formattype, Formatstyle}

Formattype:: Specifies that the argument is formatted with a different format subclass. Values range as follows:

Number: Call NumberFormat for formatting

Date: Call DateFormat for formatting

Time: Call DateFormat for formatting

Choice: Calling Choiceformat for formatting

Formatstyle:: Sets the formatting style used in Formattype. Values range as follows:

Short

Medium

Long

Full

Integer

Currency

Percent

Subformatpattern (child format pattern, as in #.##)

Also take Str as an example, in this string:

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

2, {1,number,short} inside of the number belongs to the Formattype,short is belong to Formatstyle.

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

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

Instance:

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

The code is as follows:

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, the format of the string, two single quotes to represent a single quotation mark, a single single quotation mark will be omitted, unless the Chinese single quotes will not be omitted, such as:

The code is as follows:

String value = Messageformat.format ("Oh, {0} is ' a ' pig", "Zhangsan");

System.out.println (value); Output: Oh, Zhangsan is a pig

Add single quotes to the letter A, such as:

The code is as follows:

String value = Messageformat.format ("Oh, {0} is" ' A ' pig "," Zhangsan ");

System.out.println (value); Output: Oh, Zhangsan is ' a ' pig

If you need to display double quotes to transfer, for example: String msg = "Oh, {0} is" a "pig";

3. Single quotes invalidate the placeholders behind them, resulting in a direct output placeholder.

The code is as follows:

Messageformat.format ("{0}{1}", 1, 2); Result 12

Messageformat.format ("' {0}{1}", 1, 2); result {0}{1}

Messageformat.format ("' {0} '-{1}", 1, 2); Results {0}-2

It doesn't matter if you use double quotes and two single quotes, such as

The code is as follows:

String value = Messageformat.format ("Oh, ' {0} ' is a pig", "Zhangsan");

System.out.println (value); Output: Oh, ' Zhangsan ' is a pig

Another example is using the subform pattern, one more single quote:

The code is as follows:

String value = Messageformat.format ("Oh, {0,number,#.#} is good num", double.valueof ("3.1415"));

System.out.println (value); Output: Oh, 3.1 is good num

3, whether it is a quoted string or a string without quotes, the left curly braces are not supported, such as:

The code is as follows:

String value = Messageformat.format ("Oh,} is good num", double.valueof ("3.1415"));

System.out.println (value); Output: Oh,} is good num

If you use a left curly brace, an exception appears

The code is as follows:

String value = Messageformat.format ("Oh, {is good num", double.valueof ("3.1415"));

System.out.println (value); Java.lang.IllegalArgumentException:Unmatched braces in the pattern.

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

Messageformat.format ("' {' {0}} '," X-rapido "); {X-rapido}

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

The code is as follows:

String value = Messageformat.format ("Oh, {{is good num", "D");

System.out.println (value); Oh

The code is as follows:

String value = Messageformat.format ("Oh,}} is good num", "D");

System.out.println (value); Oh,}} is good num

About the Messageformat.format method:

Each time the Messageformat.format method is invoked, a new instance of the Messageformat is created, equivalent to Messageformat only once. The format method for the Messageformat class is as follows:

Code as follows:

public static string format (string pattern, Object ... arguments)

{

Messageformat temp = new Messageformat (pattern);

return Temp.format (arguments);

}

So to format a string of the same pattern multiple times, it's better to create a Messageformat instance in order to perform a format operation

The code is as follows:

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);

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.