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.