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