我是靠谱客的博主 单纯纸飞机,这篇文章主要介绍Java三个字节_过滤3个字节以上的utf-8字符,现在分享给大家,希望可以做个参考。

/**

* 过滤掉超过3个字节的UTF8字符

* @param text

* @return

* @throws UnsupportedEncodingException

*/

public static String filterOffUtf8Mb4(String text) throws UnsupportedEncodingException {

byte[] bytes = text.getBytes("utf-8");

ByteBuffer buffer = ByteBuffer.allocate(bytes.length);

int i = 0;

while (i < bytes.length) {

short b = bytes[i];

if (b > 0) {

buffer.put(bytes[i++]);

continue;

}

b += 256; // 去掉符号位

if (((b >> 5) ^ 0x6) == 0) {

buffer.put(bytes, i, 2);

i += 2;

} else if (((b >> 4) ^ 0xE) == 0) {

buffer.put(bytes, i, 3);

i += 3;

} else if (((b >> 3) ^ 0x1E) == 0) {

i += 4;

} else if (((b >> 2) ^ 0x3E) == 0) {

i += 5;

} else if (((b >> 1) ^ 0x7E) == 0) {

i += 6;

} else {

buffer.put(bytes[i++]);

}

}

buffer.flip();

return new String(buffer.array(), "utf-8");

}

https://www.cnblogs.com/zhuawang/category/434294.html

最后

以上就是单纯纸飞机最近收集整理的关于Java三个字节_过滤3个字节以上的utf-8字符的全部内容,更多相关Java三个字节_过滤3个字节以上内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(129)

评论列表共有 0 条评论

立即
投稿
返回
顶部