CharSequenceUtil新增commonPrefix与commonSuffix方法

This commit is contained in:
zzzj 2023-03-23 18:04:44 +08:00
parent 7ce0ddf343
commit 0390c8c606
2 changed files with 103 additions and 0 deletions

View File

@ -4586,4 +4586,58 @@ public class CharSequenceUtil {
return false; return false;
} }
/**
*
* @param str1 字符串1
* @param str2 字符串2
* @return 字符串1和字符串2的公共前缀
*/
public static CharSequence commonPrefix(CharSequence str1, CharSequence str2) {
if (isBlank(str1) || isBlank(str2)) {
return EMPTY;
}
int minLength = Math.min(str1.length(), str2.length());
int index = 0;
for (; index < minLength; index++) {
if (str1.charAt(index) != str2.charAt(index)) {
break;
}
}
return str1.subSequence(0, index);
}
/**
*
* @param str1 字符串1
* @param str2 字符串2
* @return 字符串1和字符串2的公共后缀
*/
public static CharSequence commonSuffix(CharSequence str1, CharSequence str2) {
if (isBlank(str1) || isBlank(str2)) {
return EMPTY;
}
int str1Index = str1.length() - 1;
int str2Index = str2.length() - 1;
for (; str1Index >= 0 && str2Index >= 0; str1Index--, str2Index--) {
if (str1.charAt(str1Index) != str2.charAt(str2Index)) {
break;
}
}
return str1.subSequence(str1Index + 1, str1.length());
}
} }

View File

@ -160,4 +160,53 @@ public class CharSequenceUtilTest {
a = null; a = null;
Assert.assertNull(CharSequenceUtil.trimToNull(a)); Assert.assertNull(CharSequenceUtil.trimToNull(a));
} }
@Test
public void commonPrefixTest() throws Exception{
// -------------------------- None match -----------------------
Assert.assertEquals("", CharSequenceUtil.commonPrefix("", "abc"));
Assert.assertEquals("", CharSequenceUtil.commonPrefix(null, "abc"));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("abc", null));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("abc", ""));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("azzzj", "bzzzj"));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("english中文", "french中文"));
// -------------------------- Matched -----------------------
Assert.assertEquals("name_", CharSequenceUtil.commonPrefix("name_abc", "name_efg"));
Assert.assertEquals("zzzj", CharSequenceUtil.commonPrefix("zzzja", "zzzjb"));
Assert.assertEquals("中文", CharSequenceUtil.commonPrefix("中文english", "中文french"));
}
@Test
public void commonSuffixTest() throws Exception{
// -------------------------- None match -----------------------
Assert.assertEquals("", CharSequenceUtil.commonSuffix("", "abc"));
Assert.assertEquals("", CharSequenceUtil.commonSuffix(null, "abc"));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("abc", null));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("abc", ""));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("zzzja", "zzzjb"));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("中文english", "中文Korean"));
// -------------------------- Matched -----------------------
Assert.assertEquals("_name", CharSequenceUtil.commonSuffix("abc_name", "efg_name"));
Assert.assertEquals("zzzj", CharSequenceUtil.commonSuffix("abczzzj", "efgzzzj"));
Assert.assertEquals("中文", CharSequenceUtil.commonSuffix("english中文", "Korean中文"));
}
} }