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
- Related articles
- When I signed up for line, I decided to send SMS for verification. After a while, it became SMS verification. I didn't receive the text message, and I don't need to fill in the verification code. Why?
- Received a text message claiming to be a loan company, saying that my son borrowed their money. Is it true?
- I lost my mobile phone and got WeChat on my new phone. What if I haven't been verified by my friends?
- What's the matter with my wife not answering the phone and texting these days?
- Why didn't Oriental Fortune send me a message?
- The other party's mobile phone number blacked me out. Can I send her a message?
- The company pays wages uniformly with bank cards. Is it okay to cross the bank?
- Contact information of Mianyang Health and Wellness Committee
- Htc g 18 suddenly failed to receive short messages. What should I do? There is no arrears, no blacklist, and the number of the information center is correct.
- How to see whether convertible bonds are won?