Java String Difference difference(String str1, String str2)

Here you can find the source of difference(String str1, String str2)

Description

difference

License

Apache License

Declaration

public static String difference(String str1, String str2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String difference(String str1, String str2) {
        if (str1 == null) {
            return str2;
        }/*from  ww  w.j a  v  a  2 s .co  m*/
        if (str2 == null) {
            return str1;
        }
        int at = indexOfDifference(str1, str2);
        if (at == -1) {
            return "";
        }
        return str2.substring(at);
    }

    public static int indexOfDifference(String str1, String str2) {
        if (str1.equals(str2)) {
            return -1;
        }
        if ((str1 == null) || (str2 == null)) {
            return 0;
        }
        int i = 0;
        for (; (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;
    }
}

Related

  1. difference(String s1, String s2)
  2. difference(String str1, String str2)
  3. difference(String str1, String str2)
  4. difference(String str1, String str2)
  5. difference(String str1, String str2)
  6. difference(String string1, String string2)
  7. difference(String xpath1, String xpath2)