StringHelper { public static int overlap(CharSequence a, CharSequence b) { if (a == null || b == null || a.length() == 0 || b.length() == 0) { return 0; } int start = Math.max(0, a.length() - b.length()); for (int i = start; i < a.length(); ++i) { int j = 0; for (; j < b.length() && i + j < a.length(); ++j) { if (a.charAt(i + j) != b.charAt(j)) { break; } } if (i + j == a.length()) { return j; } } return 0; }  static String sanitize(String s); static String escapeJava(String s); static int overlap(CharSequence a, CharSequence b); static String toTitleCase(String str);  }
@Test public void overlap() { assertEquals(2, StringHelper.overlap("abcd", "cdef")); assertEquals(0, StringHelper.overlap("abcd", "xyz")); assertEquals(1, StringHelper.overlap("a", "a")); assertEquals(1, StringHelper.overlap("ab", "b")); assertEquals(2, StringHelper.overlap("abab", "ab")); assertEquals(4, StringHelper.overlap("ababab", "abab")); assertEquals(4, StringHelper.overlap("cdabab", "abab")); assertEquals(0, StringHelper.overlap("ababcd", "abab")); assertEquals(4, StringHelper.overlap("abab", "abab")); assertEquals(3, StringHelper.overlap("abcd", "bcdefg")); assertEquals(3, StringHelper.overlap("aaaaa", "aaa")); assertEquals(0, StringHelper.overlap("aaaaab", "aaa")); assertEquals(0, StringHelper.overlap("", "a")); assertEquals(0, StringHelper.overlap("a", "")); assertEquals(0, StringHelper.overlap("", "")); assertEquals(0, StringHelper.overlap("a", null)); assertEquals(0, StringHelper.overlap(null, "a")); assertEquals(0, StringHelper.overlap(null, null)); }