过滤3个字节以上的utf-8字符
生活随笔
收集整理的這篇文章主要介紹了
过滤3个字节以上的utf-8字符
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*** 過濾掉超過3個字節(jié)的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");}
?
轉(zhuǎn)載于:https://www.cnblogs.com/coprince/p/7490109.html
總結(jié)
以上是生活随笔為你收集整理的过滤3个字节以上的utf-8字符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue.js 技术揭秘学习 (1) ne
- 下一篇: Vue.js 技术揭秘学习 (2) Vu