日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

过滤3个字节以上的utf-8字符

發布時間:2025/4/14 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 过滤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/coprince/p/7490109.html

總結

以上是生活随笔為你收集整理的过滤3个字节以上的utf-8字符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。