字符串差异对比,google diff工具

google-diff-match-patch

git :GitHub - google/diff-match-patch: Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.https://github.com/google/diff-match-patchdemo:

https://neil.fraser.name/software/diff_match_patch/demos/diff.htmlhttps://neil.fraser.name/software/diff_match_patch/demos/diff.html

测试代码:

String aa = "DiffMatchPatch dmp = new DiffMatchPatch();";
            String bb = "DiffMatchPatch String = JSONObject DiffMatchPatch();";

            DiffMatchPatch dmp = new DiffMatchPatch();
            LinkedList<DiffMatchPatch.Diff> diff = dmp.diff_main(aa,bb,false);

            diff.forEach(diff1 -> {
                System.out.println(JSONObject.toJSONString(diff1));
            });

结果:

 结果是list,并且是顺序往下,

代码:

LinkedList<DiffMatchPatch.Diff> diffs = dmp.diff_main(lastOld,last,false);  //差异结果
                List<Map<String, String>> maps = Lists.newArrayList();  //存放的结果集
                diffs.forEach(diff -> {
                    Map<String, String> map = Maps.newHashMap();
                    if(DiffMatchPatch.Operation.EQUAL.equals(diff.operation)){  //无操作
                        map.put("key",DiffMatchPatch.Operation.EQUAL.name());
                        map.put("value",diff.text);
                    }else if(DiffMatchPatch.Operation.DELETE.equals(diff.operation)){   //删除
                        map.put("key",DiffMatchPatch.Operation.DELETE.name());
                        map.put("value",diff.text);
                    }else if(DiffMatchPatch.Operation.INSERT.equals(diff.operation)){   //插入
                        map.put("key",DiffMatchPatch.Operation.INSERT.name());
                        map.put("value",diff.text);
                    }
                    maps.add(map);
                });
                item.setContents(maps);

freemarker 出引用结果,按顺序拼接:

<#list reviewItem.contents as map>
                            <#if map["key"] == "EQUAL">
                                <w:r>
                                    <w:rPr>
                                        <w:rFonts w:ascii="仿宋_GB2312" w:hAnsi="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="eastAsia"/>
                                        <w:lang w:bidi="ar"/>
                                    </w:rPr>
                                    <w:t xml:space="preserve">${map["value"]?replace('\r','<w:br/>')}</w:t>
                                </w:r>
                            </#if>
                            <#if map["key"] == "INSERT">
                                <w:r w:rsidRPr="00262298">
                                <w:rPr>
                                    <w:rFonts w:ascii="仿宋_GB2312" w:hAnsi="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="eastAsia"/>
                                    <w:color w:val="FF0000"/>
                                    <w:u w:val="single"/>
                                    <w:lang w:bidi="ar"/>
                                </w:rPr>
                                <w:t>${map["value"]?replace('\r','<w:br/>')}</w:t>
                                </w:r>
                            </#if>
                            <#if map["key"] == "DELETE">
                                <w:r w:rsidRPr="00262298">
                                <w:rPr>
                                    <w:rFonts w:ascii="仿宋_GB2312" w:hAnsi="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="eastAsia"/>
                                    <w:strike/>
                                    <w:color w:val="1A45FA"/>
                                    <w:lang w:bidi="ar"/>
                                </w:rPr>
                                <w:t>${map["value"]?replace('\r','<w:br/>')}</w:t>
                                </w:r>
                            </#if>
                        </#list>

实际结果展示(转换了pdf,因为没有字体所以是乱码):

maven引用:

<dependency>
    <groupId>org.bitbucket.cowwoc</groupId>
    <artifactId>diff-match-patch</artifactId>
    <scope>1.2</scope>
</dependency>

实际使用可以吧代码放项目。不需要maven去引用,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值