Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract the difference between two strings in Java

Hi I have two strings :

    String hear = "Hi My name is Deepak"
            + "\n"
            + "How are you ?"
            + "\n"
            + "\n"
            + "How is everyone";
    String dear = "Hi My name is Deepak"
            + "\n"
            + "How are you ?"
            + "\n"
            + "Hey there \n"
            + "How is everyone";

I want to get what is not present in the hear string that is "Hey There \n". I found a method , but it fails for this case :

static String strDiffChop(String s1, String s2) {
    if (s1.length() > s2.length()) {
        return s1.substring(s2.length() - 1);
    } else if (s2.length() > s1.length()) {
        return s2.substring(s1.length() - 1);
    } else {
        return "";
    }
}

Can any one help ?

like image 771
N Deepak Prasath Avatar asked Aug 20 '13 20:08

N Deepak Prasath


People also ask

How do you find the difference between two strings?

difference() returns the difference between two strings, returning the portion of the second string, which starts to differ from the first. StringUtils. indexOfDifference() returns the index at which the second string starts to diverge from the first.

Can we compare 2 strings using == in Java?

To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.

How do you compare the performance of two strings?

Compare Strings Using the Equals() Method equals() method is the Object class method, String class overrides it. equals() method compare two strings for value equality, whether they are logically equal. equals() method in String class takes another string as a parameter and compares it with the specified string.


2 Answers

google-diff-match-patch

The Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronizing plain text.

Diff:

Compare two blocks of plain text and efficiently return a list of differences.

Match:

Given a search string, find its best fuzzy match in a block of plain text. Weighted for both accuracy and location.

Patch:

Apply a list of patches onto plain text. Use best-effort to apply patch even when the underlying text doesn't match.

Currently available in Java, JavaScript, Dart, C++, C#, Objective C, Lua and Python. Regardless of language, each library features the same API and the same functionality. All versions also have comprehensive test harnesses.

There is a Line or word diffs wiki page which describes how to do line-by-line diffs.

like image 151
Mike Samuel Avatar answered Sep 22 '22 08:09

Mike Samuel


One can use the StringUtils from Apache Commons. Here is the StringUtils API.

public static String difference(String str1, String str2) {
    if (str1 == null) {
        return str2;
    }
    if (str2 == null) {
        return str1;
    }
    int at = indexOfDifference(str1, str2);
    if (at == -1) {
        return EMPTY;
    }
 return str2.substring(at);
}
public static int indexOfDifference(String str1, String str2) {
    if (str1 == str2) {
        return -1;
    }
    if (str1 == null || str2 == null) {
        return 0;
    }
    int i;
    for (i = 0; i < str1.length() && i < str2.length(); ++i) {
        if (str1.charAt(i) != str2.charAt(i)) {
            break;
        }
    }
    if (i < str2.length() || i < str1.length()) {
        return i;
    }
    return -1;
}
like image 29
Fly Avatar answered Sep 22 '22 08:09

Fly