Here you can find the source of difference(String str1, String str2)
public static String difference(String str1, String str2)
//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; } }