Java Magic Hall: A tentative study of Messageformat.format

Source: Internet
Author: User
Tags dateformat string format

First, preface

When I started to move from. NET to Java, I always feel that String.Format is not used to it, I hope the format template will be {0}, this is {1 }'s cat.{ 1},this is {0}' s dog. Instead of the%1$s , this is the%2$s's cat.%2$s,this is%1 $ s 's dog. 。 Later found that Java.text.MessageFormat.format can meet my small wish.

Second, static method Java.text.MessageFormat.format

Method definition:

String Messageformat.format (String fmt, Object...args)

Enter the FMT as the Messageformat mode parameter.

Three, Messageformat mode

Format: Argumentindex[,formattype[,formatstyle]]

Argumentindex , which is the index of the entry position starting at 0.

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

formattype , sets the formatting style used in Formattype. The value range is as follows:

Short,medium,long,full,integer,currency,percent,subformpattern (sub-format pattern, shaped like #.##)

Note: formattype and Formatstyle are mainly used for formatting dates, numbers, percentages, and so on.

Example--format the number 1.23 as 1.2:

Double 1.23  = Messageformat.format ("{0,number,#.#}", num);

Iv. Messageformat points of attention

1. Two single quotes represent a single quotation mark, and only one single quotation mark is ignored.

2. Single quotation marks invalidate the placeholders that follow them, resulting in a direct output placeholder.

Messageformat.format ("{0}{1}"12//  results Messageformat.format ("' {0}{1}"12//  result {0}{1} Messageformat.format ("' {0} ' {1}"12// result {0}

So it can be used to output left curly braces (single-write left curly braces will error, while single-write right curly brackets will output normally)

Messageformat.format ("' {' {0}}"2//  result {2

So the example in the preface should be written as

{0 This is {1}'s cat. {1}, This is {0}'s dog.

V. Class-Level relationships

|--Java.text.MessageFormat

Top level abstract class java.text.format-| |--java.text.choiceformat

|--java.text.numberformat-|

| |--java.text.decimalformat

|

|--java.text.dateformat-java.text.simpledateformat

Vi. Performance Issues

Because the static method Messageformat.format Internal is

 Public Static string Format (string pattern, Object ... arguments)   {      new  Messageformat (pattern);       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.

Vii. Summary

For simple formatting or string assembly, the Messageformat.format method is more convenient to use, but to format more rich words if you use the String.Format method !

Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/4095059.html ^_^ Fat Boy John

Viii. Reference                                  

http://zqc-0101.iteye.com/blog/114014

Java Magic Hall: A tentative study of Messageformat.format

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.