mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
c7be2191ae
commit
7db5d0f31f
@ -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长度)
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user