add method

This commit is contained in:
Looly 2023-04-17 10:05:27 +08:00
parent c7be2191ae
commit 7db5d0f31f
2 changed files with 20 additions and 0 deletions

View File

@ -3111,6 +3111,18 @@ public class CharSequenceUtil extends StrChecker {
return cs == null ? 0 : cs.length();
}
/**
* 获取字符串的Unicode字符长度如果为{@code null}返回0<br>
* Unicode字符长度指实际Unicode字符个数如emoji算一个字符
*
* @param cs a 字符串
* @return 字符串的长度如果为{@code null}返回0
* @since 6.0.0
*/
public static int codeLength(final CharSequence cs) {
return cs == null ? 0 : cs.toString().codePointCount(0, cs.length());
}
/**
* 给定字符串转为bytes后的byte数byte长度
*

View File

@ -284,4 +284,12 @@ public class CharSequenceUtilTest {
// Expect common suffix: { space * 10 }
Assertions.assertEquals(CharSequenceUtil.repeat(CharSequenceUtil.SPACE, 10), CharSequenceUtil.commonSuffix(str1, str2));
}
@Test
void codeLengthTest() {
String a = "🍒🐽";
final int i = StrUtil.codeLength(a);
Assertions.assertEquals(4, a.length());
Assertions.assertEquals(2, i);
}
}