Java uses the MessageFormat object to implement formatting similar to the C # string. Format Method

Source: Internet
Author: User
Tags key string

Java uses the MessageFormat object to implement formatting similar to the C # string. Format Method
We often use string when writing C # code. format ("string to be formatted {0}, {1 },.... ", parameter 1, parameter 2 ,...), this method makes the code more intuitive and clear. Java string recently used. format and c # key string. the Format usage is different. It is a string in java. format is more similar to the sprintf () method in C language, for example: String str = null; str = String. format ("Hello, % s", "world"); // format the String, but if we write str = String. format ("Hello, {0}", "world"); the output is: Hello, {0} is not the final result we want to find that the format method of the MessageFormat object in java can achieve the same effect in c #. Test: import java. text. messageFormat; public class Test {public static void main (String args []) throws Exception {String [] arr = {"Zhang San", "10000"}; String str = MessageFormat. format ("name = {0}, salary = {1}", arr); System. out. println (str); // name = Zhang San, salary = 10000} output: name = Zhang San, salary = 10,000

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.