标签:
原文:http://www.oschina.net/code/snippet_564772_13507
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
static public 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 ^ 0xC0) >> 4 == 0) { buffer.put(bytes, i, 2); i += 2; } else if ((b ^ 0xE0) >> 4 == 0) { buffer.put(bytes, i, 3); i += 3; } else if ((b ^ 0xF0) >> 4 == 0) { i += 4; } } buffer.flip(); return new String(buffer.array(), "utf-8"); } |
标签:
原文地址:http://www.cnblogs.com/zhuawang/p/4204121.html