如何在Java中执行字符串Diffs? [英] How to perform string Diffs in Java?

查看:123
本文介绍了如何在Java中执行字符串Diffs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java字符串之间执行Diffs。我希望能够使用原始字符串和diff版本重建字符串。有没有人用Java做过这个?您使用什么库?

I need to perform Diffs between Java strings. I would like to be able to rebuild a string using the original string and diff versions. Has anyone done this in Java? What library do you use?

String a1; // This can be a long text
String a2; // ej. above text with spelling corrections
String a3; // ej. above text with spelling corrections and an additional sentence

Diff diff = new Diff();
String differences_a1_a2 = Diff.getDifferences(a,changed_a);
String differences_a2_a3 = Diff.getDifferences(a,changed_a);    
String[] diffs = new String[]{a,differences_a1_a2,differences_a2_a3};
String new_a3 = Diff.build(diffs);
a3.equals(new_a3); // this is true


推荐答案

这个库似乎做了技巧: google-diff-match-patch 。它可以根据差异创建补丁字符串,并允许重新应用补丁。

This library seems to do the trick: google-diff-match-patch. It can create a patch string from differences and allow to reapply the patch.

编辑:另一种解决方案可能是 https://code.google.com/p/java-diff-utils/

edit: Another solution might be to https://code.google.com/p/java-diff-utils/

这篇关于如何在Java中执行字符串Diffs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆