Joke Collection Website - Public benefit messages - The replacement problem of Java string templates, I am a beginner, and the teacher did not teach about sets. How to do it?

The replacement problem of Java string templates, I am a beginner, and the teacher did not teach about sets. How to do it?

import java.util.Properties;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class StringUtils {

public static String composeMessage(String template,Properties data) throws Exception {

Pattern p = Pattern.compile("\\$\\{( \\w+)\\}", Pattern.MULTILINE);

Matcher m = p.matcher(template);

while (m.find()) {

template = template.replace(m.group(), data.getProperty(m.group(1)));

}

return template;

}

/**

* @param args

*/

public static void main(String[] args) {

String template="Dear customer ${customerName}! The consumption amount this time is ${amount}, and the balance on your account ${accountNumber} is ${balance}";< /p>

Properties prop = new Properties();

prop.put("customerName", "Zhang San");

prop.put("amount", "10");

prop.put("accountNumber", "No.123456");

prop.put("balance", "80.5");

try {

System.out.println(composeMessage(template, prop));

} catch (Exception e) {

e. printStackTrace();

}

}

}

//Use regular expressions to match your variables