CharSequences { public static CharSequence getCommonPrefix(CharSequence first, CharSequence second) { int minLength = Math.min(first.length(), second.length()); for (int i = 0; i < minLength; i++) { if (first.charAt(i) != second.charAt(i)) { return first.subSequence(0, i); } } return first.subSequence(0, minLength); } CharSequences(); static Iterable<CharSequence> generateSuffixes(final CharSequence input); static Iterable<CharSequence> generatePrefixes(final CharSequence input); static CharSequence getCommonPrefix(CharSequence first, CharSequence second); static CharSequence getSuffix(CharSequence input, int startIndex); static CharSequence getPrefix(CharSequence input, int endIndex); static CharSequence subtractPrefix(CharSequence main, CharSequence prefix); static CharSequence concatenate(final CharSequence first, final CharSequence second); static CharSequence reverse(CharSequence input); static CharSequence fromCharArray(final char[] characters); static char[] toCharArray(CharSequence charSequence); static String toString(CharSequence charSequence);  }
@Test public void testGetCommonPrefix() throws Exception { Assert.assertEquals("BAN", CharSequences.getCommonPrefix("BANANA", "BANDANA")); Assert.assertEquals("BAN", CharSequences.getCommonPrefix("BAN", "BANDANA")); Assert.assertEquals("BAN", CharSequences.getCommonPrefix("BANANA", "BAN")); Assert.assertEquals("", CharSequences.getCommonPrefix("BANANA", "ABANANA")); Assert.assertEquals("", CharSequences.getCommonPrefix("", "BANANA")); Assert.assertEquals("", CharSequences.getCommonPrefix("BANANA", "")); Assert.assertEquals("T", CharSequences.getCommonPrefix("TOAST", "TEAM")); }