import java.text.ParseException; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; public class StringTemplateUtils { public static final String DEF_REGEX="\\{(.+?)\\}"; public static String render(String template, Map<String, String> data) { return render(template,data,DEF_REGEX); } public static String render(String template, Map<String, String> data,String regex) { if(StringUtils.isBlank(template)){ return ""; } if(StringUtils.isBlank(regex)){ return template; } if(data == null || data.size() == 0){ return template; } try { StringBuffer sb = new StringBuffer(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(template); while (matcher.find()) { String name = matcher.group(1);// 鍵名 String value = data.get(name);// 鍵值 if (value == null) {value = "";} matcher.appendReplacement(sb, value); } matcher.appendTail(sb); return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return template; } public static void main(String args[]) throws ParseException { String template="您提現{borrowAmount}元至尾號{tailNo}的請求失敗,您能夠從新提交提款申請。"; Map<String, String> data = new HashMap<String, String>(); data.put("borrowAmount", "1000.00"); data.put("tailNo", "1234"); System.out.println(render(template,data)); } }