如何在 Java 中执行字符串差异?

新手上路,请多包涵

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

 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

原文由 Sergio del Amo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 648
2 个回答

Apache Commons 有字符串差异

org.apache.commons.lang.StringUtils

 StringUtils.difference("foobar", "foo");

原文由 Paul Whelan 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题