gpt4 book ai didi

java - 如何按字节大小截断包含表情符号的字符串

转载 作者:行者123 更新时间:2023-11-30 07:53:28 34 4
gpt4 key购买 nike

我想将 UTF-8 字符集大小的字符串限制为 30 个字节,我找到了解决方案 this

所以我创建了一个基于此的方法

public static String truncateTextByByteLimit(String message, int byteLimit) {
String result = "";
try {
Charset utf8Charset = Charset.forName("UTF-8");
CharsetDecoder cd = utf8Charset.newDecoder();
byte[] utf8Bytes = message.getBytes(utf8Charset);
System.out.println("check message: " + message + " /length: " +message.length()+ " //byte length: " + utf8Bytes.length + "/limit: " + byteLimit + " /codePoint: " +message.codePointCount(0, message.length()));
ByteBuffer bb = ByteBuffer.wrap(utf8Bytes, 0, byteLimit);
CharBuffer cb = CharBuffer.allocate(byteLimit);
// Ignore an incomplete character
cd.onMalformedInput(CodingErrorAction.IGNORE);
cd.decode(bb, cb, true);
cd.flush(cb);
result = new String(cb.array(), 0, cb.position());
if (result.length()<=0) {
return truncateTextByByteLimit(message, (byteLimit+1));
} else {
return result;
}
} catch (Exception e) {
e.printStackTrace();

return message;
}
}

问题是当我用表情符号测试字符串时,如下所示:System.out.println(truncateTextByByteLimit("让我们\uD83D\uDE09", 30));

显示错误

java.lang.IndexOutOfBoundsException
at java.nio.ByteBuffer.wrap(ByteBuffer.java:371)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我的调试消息显示检查消息:让我们😉/length: 8//字节长度: 10/limit: 30/codePoint: 7

当我使用相同的消息进行测试且 byteLimit 小于或等于 10 时,它可以正常工作,不会出现错误...

所以我不明白为什么它显示java.lang.IndexOutOfBoundsException

最佳答案

ByteBuffer#wrap has a limitation允许的长度。

The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer's limit will be set to offset + length.

为了解决这个问题,您需要取两个长度中较小的一个 - 要么它就是您的绝对最大值 byteLimit ,或者它将是 utf8Bytes 的大小数组。

ByteBuffer.wrap(utf8Bytes, 0, Math.min(utf8Bytes.length, byteLimit));

关于java - 如何按字节大小截断包含表情符号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006160/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com