mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
commonPrefix与commonSuffix支持空字符串(前缀/后缀)匹配
This commit is contained in:
parent
7bed31e13f
commit
8b0b21f742
@ -4594,7 +4594,7 @@ public class CharSequenceUtil {
|
||||
* @return 字符串1和字符串2的公共前缀
|
||||
*/
|
||||
public static CharSequence commonPrefix(CharSequence str1, CharSequence str2) {
|
||||
if (isBlank(str1) || isBlank(str2)) {
|
||||
if (isEmpty(str1) || isEmpty(str2)) {
|
||||
return EMPTY;
|
||||
}
|
||||
final int minLength = Math.min(str1.length(), str2.length());
|
||||
@ -4618,7 +4618,7 @@ public class CharSequenceUtil {
|
||||
* @return 字符串1和字符串2的公共后缀
|
||||
*/
|
||||
public static CharSequence commonSuffix(CharSequence str1, CharSequence str2) {
|
||||
if (isBlank(str1) || isBlank(str2)) {
|
||||
if (isEmpty(str1) || isEmpty(str2)) {
|
||||
return EMPTY;
|
||||
}
|
||||
int str1Index = str1.length() - 1;
|
||||
|
@ -183,6 +183,14 @@ public class CharSequenceUtilTest {
|
||||
|
||||
Assert.assertEquals("中文", CharSequenceUtil.commonPrefix("中文english", "中文french"));
|
||||
|
||||
// { space * 10 } + "abc"
|
||||
final String str1 = CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 10) + "abc";
|
||||
|
||||
// { space * 5 } + "efg"
|
||||
final String str2 = CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 5) + "efg";
|
||||
|
||||
// Expect common prefix: { space * 5 }
|
||||
Assert.assertEquals(CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 5), CharSequenceUtil.commonPrefix(str1, str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -207,6 +215,14 @@ public class CharSequenceUtilTest {
|
||||
|
||||
Assert.assertEquals("中文", CharSequenceUtil.commonSuffix("english中文", "Korean中文"));
|
||||
|
||||
// "abc" + { space * 10 }
|
||||
final String str1 = "abc" + CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 10);
|
||||
|
||||
// "efg" + { space * 15 }
|
||||
final String str2 = "efg" + CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 15);
|
||||
|
||||
// Expect common suffix: { space * 10 }
|
||||
Assert.assertEquals(CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 10), CharSequenceUtil.commonSuffix(str1, str2));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user