Java 字符串模板和内容对比得到模板变量值对应的内容值

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

对于日常开发中可能大家都会遇到此类问题,两个字符串进行对比得出不同字符。此次在开发中遇到,就记录下来。基本思路是替换模板内容的变量值将相同部分提取出来,将提取出来的相同部分内容和实际内容循环截取掉相同部分。取出模板变量值对应的内容值


提示:以下是本篇文章正文内容,下面案例可供参考

一、使用步骤

1.对比模板与内容

代码如下(示例):

// 模板
String a = "您在这个日期{{date}}找到我,我将给你RMB {{money_num}}, 相逢即使缘分{{text}},联系电话:{{number}}。";
// 内容
String b = "【美丽心情】您在这个日期2021-11-01 12:00:00找到我,我将给你RMB 0000.001, 相逢即使缘分你说是不是?,联系电话:88888888";

2.工具类

代码如下(示例):

// 获得对比结果
public static List<String> getReplaceList(String a,String b){
        String aTemplate = filterString(a);
        String bTemplate = filterString(b);
        int index = aTemplate.indexOf(System.lineSeparator());
        String aTmpe = aTemplate.substring(index + 1);
        String bTmpe = bTemplate.replace("【美丽心情】", "").trim();
        String regex = "(\\{\\{[^}}]*}})";
        String[] ptTemplate = aTmpe.replaceAll(regex, "@=").split("@=");
        String replace = "";
        for (int i = 0; i < ptTemplate.length; i++) {
            if(ptTemplate[i]==null || ptTemplate[i].equals(" "))continue;
            if(replace.equals("")){
                replace = bTmpe.replace(ptTemplate[i], "@=");
            }else {
                replace = replace.replace(ptTemplate[i], "@=");
            }
        }
        List<String> aList = new ArrayList<>();
        String[] split = replace.split("@=");
        for (int i =0; i<split.length; i++){
            if(split[i] == "" || split[i].equals("")) continue;
            aList.add(split[i]);
        }
        return aList;
}
    
 // 去掉内容中的换行符
 public static String filterString(String  str){
        if(str == null || str.equals("")) return null;
        String regEx= "[\\r\\n]";
        Pattern   p   =   Pattern.compile(regEx);
        Matcher   m   =   p.matcher(str);
        return m.replaceAll(" ").trim();
}

// 检索时,转换特殊字符
public static String escapeQueryChars(String s) {
     if (StringUtils.isBlank(s)) {
         return s;
     }
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < s.length(); i++) {
         char c = s.charAt(i);
         // These characters are part of the query syntax and must be escaped
         if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')'
                 || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"'
                 || c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
                 || c == '|' || c == '&' || c == ';' || c == '/' || c == '.'
                 || c == '$' || Character.isWhitespace(c)) {
             sb.append('\\');
         }
         sb.append(c);
     }
     return sb.toString();
}
 

3.调用main方法

最后调用main方法,调用工具类

public static void main(String[] args) {
	String a = "您在这个日期{{date}}找到我,我将给你RMB {{money_num}}, 相逢即使缘分{{text}},联系电话:{{number}}。";
    String b = "【美丽心情】您在这个日期2021-11-01 12:00:00找到我,我将给你RMB 0000.001, 相逢即使缘分你说是不是?,联系电话:88888888";

    if(a!=null && !a.equals("") && b!=null && !b.equals("")) {
        List<String> replaceList = getReplaceList(a, b);
        for (int i =0; i<replaceList.size(); i++){
            System.out.println("num"+(i+1)+ "=========="+replaceList.get(i));
        }
    }
}

4.打印结果

打印的结果如下:
在这里插入图片描述


总结

如果有帮助到您,还请关注收藏一下,不定时更新。如果您有更好的方法,也可在文章下方进行留言,大家一起沟通。谢谢!

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值