mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!84 support substring by code point
Merge pull request !84 from Ease/v5-dev
This commit is contained in:
commit
05aff7f94b
@ -1579,6 +1579,33 @@ public class StrUtil {
|
|||||||
return str.toString().substring(fromIndex, toIndex);
|
return str.toString().substring(fromIndex, toIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过CodePoint截取字符串,可以截断Emoji
|
||||||
|
*
|
||||||
|
* @param str String
|
||||||
|
* @param fromIndex 开始的index(包括)
|
||||||
|
* @param toIndex 结束的index(不包括)
|
||||||
|
* @return 字串
|
||||||
|
*/
|
||||||
|
public static String subByCodePoint(CharSequence str, int fromIndex, int toIndex) {
|
||||||
|
if (isEmpty(str)) {
|
||||||
|
return str(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromIndex < 0 || fromIndex > toIndex) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromIndex == toIndex) {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int subLen = toIndex - fromIndex;
|
||||||
|
str.toString().codePoints().skip(fromIndex).limit(subLen).forEach(v -> sb.append(Character.toChars(v)));
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 截取部分字符串,这里一个汉字的长度认为是2
|
* 截取部分字符串,这里一个汉字的长度认为是2
|
||||||
*
|
*
|
||||||
|
@ -219,6 +219,20 @@ public class StrUtilTest {
|
|||||||
Assert.assertEquals("ghigh", pre);
|
Assert.assertEquals("ghigh", pre);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void subByCodePointTest() {
|
||||||
|
// 🤔👍🍓🤔
|
||||||
|
String test = "\uD83E\uDD14\uD83D\uDC4D\uD83C\uDF53\uD83E\uDD14";
|
||||||
|
|
||||||
|
// 不正确的子字符串
|
||||||
|
String wrongAnswer = StrUtil.sub(test, 0, 3);
|
||||||
|
Assert.assertNotEquals("\uD83E\uDD14\uD83D\uDC4D\uD83C\uDF53", wrongAnswer);
|
||||||
|
|
||||||
|
// 正确的子字符串
|
||||||
|
String rightAnswer = StrUtil.subByCodePoint(test, 0, 3);
|
||||||
|
Assert.assertEquals("\uD83E\uDD14\uD83D\uDC4D\uD83C\uDF53", rightAnswer);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void subBeforeTest() {
|
public void subBeforeTest() {
|
||||||
String a = "abcderghigh";
|
String a = "abcderghigh";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user