String template replacement method messageformat. Format (--)

Source: Internet
Author: User

I made a template for configuring emails and text messages. Many parameters need to be replaced in the template, such as "User Name" and "date. However, there is no better way to replace parameters. Currently, only one simple method can be used. This method is too rigid, and the corresponding parameter must be inCodeIf you add a parameter to the template, you must modify the code in the Java class. Now, we can work together on a temporary basis. We will come up with a better way to achieve this in the future. The following code is used:

In fact, this implementation uses the messageformat class;

String content = "AB, CC, {name}, {password}, {date}, DD, FF ";

String Array [] = {username, password, format. Format (new date ())};
Content = messageformat. Format (content, array );

Explanation:

The parameters in {} need to be replaced in content, and the corresponding parameters to be replaced are stored in the array. When using the messageformat method, you must match the number of these parameters correctly and specify the number order. Otherwise, an error occurs. In this way, the parameter is replaced. Very simple and rigid.

Messageformat: From the java. Text package;

We can open JDK by ourselves.Source codeRead his implementation method.

Related Article

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.