mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
CharSequenceUtil新增commonPrefix与commonSuffix方法
This commit is contained in:
parent
7ce0ddf343
commit
0390c8c606
@ -4586,4 +4586,58 @@ public class CharSequenceUtil {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -160,4 +160,53 @@ public class CharSequenceUtilTest {
|
||||
a = null;
|
||||
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中文"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user