字符串模板替换

版权声明:本文为博主原创文章,未经博主允许不得转载,转载请附原文链接说明出处。 https://blog.csdn.net/xupeng874395012/article/details/87347407

1、使用正则便打算来替换

 public  static String renderString(String content, Map<String, String> map){
        Set<Map.Entry<String, String>> sets = map.entrySet();
        for(Map.Entry<String, String> entry : sets) {
            String regex = "\\$\\{" + entry.getKey() + "\\}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(content);
            content = matcher.replaceAll(entry.getValue());
        }
        return content;
    }
    public static void main(String[] args){
        Map<String,String> map=new HashMap<>();
        map.put("day","111");
        System.out.println(renderString("**免${day}天息费",map));
    }

猜你喜欢

转载自blog.csdn.net/xupeng874395012/article/details/87347407