在Java中提取两个字符串之间的差异

N D*_*ath 18 java string compare

嗨我有两个字符串:

    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";
Run Code Online (Sandbox Code Playgroud)

我想得到听到的字符串中没有的内容"嘿那里\n".我找到了一个方法,但在这种情况下失败了:

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 "";
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮忙吗?

Mik*_*uel 25

谷歌的Diff-比赛补丁

Diff Match和Patch库提供了强大的算法来执行同步纯文本所需的操作.

DIFF:

比较两个纯文本块并有效地返回差异列表.

比赛:

给定搜索字符串,在纯文本块中找到其最佳模糊匹配.为准确性和位置加权.

补丁:

将修补程序列表应用于纯文本.即使基础文本不匹配,也要尽最大努力应用补丁.

目前提供Java,JavaScript,Dart,C++,C#,Objective C,Lua和Python.无论语言如何,每个库都具有相同的API和相同的功能.所有版本都有全面的测试工具.

有一个Line或word diffs wiki页面,它描述了如何进行逐行差异.

  • 多棒的lib.谢谢. (2认同)

Fly*_*Fly 7

可以使用StringUtils来自Apache Commons的.这是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;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经使用StringTokenizer来找到解决方案。下面是代码片段

public static List<String> findNotMatching(String sourceStr, String anotherStr){
    StringTokenizer at = new StringTokenizer(sourceStr, " ");
    StringTokenizer bt = null;
    int i = 0, token_count = 0;
    String token = null;
    boolean flag = false;
    List<String> missingWords = new ArrayList<String>();
    while (at.hasMoreTokens()) {
        token = at.nextToken();
        bt = new StringTokenizer(anotherStr, " ");
        token_count = bt.countTokens();
        while (i < token_count) {
            String s = bt.nextToken();
            if (token.equals(s)) {
                flag = true;
                break;
            } else {
                flag = false;
            }
            i++;
        }
        i = 0;
        if (flag == false)
            missingWords.add(token);
    }
    return missingWords;
}
Run Code Online (Sandbox Code Playgroud)