StringComparisonUtil implements Serializable { public static StringBuilder getCommonCharacters(final String string1, final String string2, final int distanceSep) { final StringBuilder returnCommons = new StringBuilder(); int[] cpArray1 = stringToCodePointArray(string1); int[] cpArray2 = stringToCodePointArray(string2); for (int i = 0; i < cpArray1.length; i++) { final int codePoint = cpArray1[i]; boolean foundIt = false; for (int j = Math.max(0, i - distanceSep); !foundIt && j < Math.min(i + distanceSep + 1, cpArray2.length); j++) { if (cpArray2[j] == codePoint) { foundIt = true; returnCommons.append(String.valueOf(Character.toChars(codePoint))); cpArray2[j] = -1; } } } return returnCommons; } private  StringComparisonUtil(); static int difference(String str1, String str2); static StringBuilder getCommonCharacters(final String string1, final String string2, final int distanceSep); static int getPrefixLength(final String string1, final String string2);  }
@Test public void testGetCommonCharacters() { assertEquals("abc", StringComparisonUtil.getCommonCharacters(str1, str2, 1).toString()); assertEquals("abc", StringComparisonUtil.getCommonCharacters(str1, str3, 1).toString()); assertEquals("ab", StringComparisonUtil.getCommonCharacters(str1, str4, 1).toString()); assertEquals("bc", StringComparisonUtil.getCommonCharacters(str1, str5, 1).toString()); assertEquals(" ", StringComparisonUtil.getCommonCharacters(" ", " ", 1).toString()); assertEquals("", StringComparisonUtil.getCommonCharacters(null, " ", 1).toString()); assertEquals("", StringComparisonUtil.getCommonCharacters(null, null, 1).toString()); assertEquals(str7, StringComparisonUtil.getCommonCharacters(str6, str7, 1).toString()); assertEquals(str9, StringComparisonUtil.getCommonCharacters(str8, str9, 1).toString()); assertEquals("𠀠𠀡", StringComparisonUtil.getCommonCharacters("𠀠𠀡𠀢", "𠀠a𠀡", 2).toString()); assertEquals("𠀐𠀑𠀒", StringComparisonUtil.getCommonCharacters("𠀐𠀑𠀒𠀓𠀔", "a𠀐𠀒c𠀑", 3).toString()); }