Java String Difference difference(String str1, String str2)

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

Description

difference

License

Open Source License

Declaration

public static String difference(String str1, String str2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static final int INDEX_NOT_FOUND = -1;
    public static final String EMPTY = "";

    public static String difference(String str1, String str2) {
        if (str1 == null) {
            return str2;
        }//from ww  w.jav a  2 s .co  m
        if (str2 == null) {
            return str1;
        }
        int at = indexOfDifference(str1, str2);
        if (at == INDEX_NOT_FOUND) {
            return EMPTY;
        }
        return str2.substring(at);
    }

    public static int indexOfDifference(CharSequence cs1, CharSequence cs2) {
        if (cs1 == cs2) {
            return INDEX_NOT_FOUND;
        }
        if (cs1 == null || cs2 == null) {
            return 0;
        }
        int i;
        for (i = 0; i < cs1.length() && i < cs2.length(); ++i) {
            if (cs1.charAt(i) != cs2.charAt(i)) {
                break;
            }
        }
        if (i < cs1.length() || i < cs2.length()) {
            return i;
        }
        return INDEX_NOT_FOUND;
    }

    public static int indexOfDifference(CharSequence... css) {
        if (css == null || css.length <= 1) {
            return INDEX_NOT_FOUND;
        }
        boolean anyStringNull = false;
        boolean allStringsNull = true;
        int arrayLen = css.length;
        int shortestStrLen = Integer.MAX_VALUE;
        int longestStrLen = 0;

        for (int i = 0; i < arrayLen; i++) {
            if (css[i] == null) {
                anyStringNull = true;
                shortestStrLen = 0;
            } else {
                allStringsNull = false;
                shortestStrLen = Math.min(css[i].length(), shortestStrLen);
                longestStrLen = Math.max(css[i].length(), longestStrLen);
            }
        }
        if (allStringsNull || longestStrLen == 0 && !anyStringNull) {
            return INDEX_NOT_FOUND;
        }
        if (shortestStrLen == 0) {
            return 0;
        }

        int firstDiff = -1;
        for (int stringPos = 0; stringPos < shortestStrLen; stringPos++) {
            char comparisonChar = css[0].charAt(stringPos);
            for (int arrayPos = 1; arrayPos < arrayLen; arrayPos++) {
                if (css[arrayPos].charAt(stringPos) != comparisonChar) {
                    firstDiff = stringPos;
                }
            }
            if (firstDiff != -1) {
                break;
            }
        }

        if (firstDiff == -1 && shortestStrLen != longestStrLen) {
            return shortestStrLen;
        }
        return firstDiff;
    }
}

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)
  8. differenceAt(String s1, String s2)