How to perform string Diffs in Java?


Java Problem Overview

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 =;
a3.equals(new_a3); // this is true

Java Solutions

Solution 1 - Java

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.

edit: Another solution might be to

Solution 2 - Java

Apache Commons has String diff


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

Solution 3 - Java

The java diff utills library might be useful.

Solution 4 - Java

As Torsten Says you can use


System.err.println(StringUtils.getLevenshteinDistance("foobar", "bar"));

Solution 5 - Java

If you need to deal with differences between big amounts of data and have the differences efficiently compressed, you could try a Java implementation of xdelta, which in turn implements RFC 3284 (VCDIFF) for binary diffs (should work with strings too).

Solution 6 - Java

Use the Levenshtein distance and extract the edit logs from the matrix the algorithm builds up. The Wikipedia article links to a couple of implementations, I'm sure there's a Java implementation among in.

Levenshtein is a special case of the Longest Common Subsequence algorithm, you might also want to have a look at that.

Solution 7 - Java

Apache Commons Text now has StringsComparator:

StringsComparator c = new StringsComparator(s1, s2);
c.getScript().visit(new CommandVisitor<Character>() {

    public void visitKeepCommand(Character object) {
        System.out.println("k: " + object);

    public void visitInsertCommand(Character object) {
        System.out.println("i: " + object);

    public void visitDeleteCommand(Character object) {
        System.out.println("d: " + object);

Solution 8 - Java

public class Stringdiff {
public static void main(String args[]){
public static String strcheck(String str1,String str2){
		return "Invalid";
	int num=diffcheck1(str1, str2);
		return "Empty";
		return str1.substring(num);
		return str2.substring(num);

public static int diffcheck1(String str1,String str2)
	int i;
	String str;
	String strn;
	for(i=0;i<str.length() && i<strn.length();i++){
				return i;
			return i;
	return -1;



All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSergio del AmoView Question on Stackoverflow
Solution 1 - JavabernardnView Answer on Stackoverflow
Solution 2 - JavaPaul WhelanView Answer on Stackoverflow
Solution 3 - JavadnaumenkoView Answer on Stackoverflow
Solution 4 - JavaPaul WhelanView Answer on Stackoverflow
Solution 5 - JavaAlexanderView Answer on Stackoverflow
Solution 6 - JavaTorsten MarekView Answer on Stackoverflow
Solution 7 - JavaAhmed AshourView Answer on Stackoverflow
Solution 8 - JavaSandeep Raj UrsView Answer on Stackoverflow