Java按字节截取字符串(GBK编码、UTF-8编码实现)

时间:2023-01-06 21:15:47
 1 package FileDemo;
 2 
 3 import java.io.IOException;
 4 
 5 public class CutStringTest {
 6 
 7     /**
 8      * @param args
 9      * @throws IOException
10      */
11     public static void main(String[] args) throws IOException {
12 
13         String str = "ab你好cd谢谢";
14         /*byte buf[]=str.getBytes("GBK");
15         for(byte ch:buf){
16             System.out.println(Integer.toBinaryString(ch));
17         }*/
18         int len = str.getBytes("gbk").length;
19         for (int x = 0; x < len; x++) {
20             System.out.println("截取" + (x + 1) + "字节结果时:"
21                     + cutStringByByte(str, x + 1));
22         }
23         String str1 = "ab你好cd杮";
24         int len1 = str.getBytes("gbk").length;
25         for (int x = 0; x < len1; x++) {
26             System.out.println("截取" + (x + 1) + "字节结果时:"
27                     + cutStringByU8(str1, x + 1));
28         }
29     }
30 
31     // 使用UTF-8编码表进行截取字符串,一个汉字对应三个负数,一个英文字符对应一个正数
32     private static String cutStringByU8(String str, int len) throws IOException {
33 
34         byte[] buf = str.getBytes("utf-8");
35         int count = 0;
36         for (int x = len - 1; x >= 0; x--) {
37             if (buf[x] < 0) {
38                 count++;
39             } else {
40                 break;
41             }
42         }
43         if (count % 3 == 0) {
44             return new String(buf, 0, len, "utf-8");
45         } else if (count % 3 == 1) {
46             return new String(buf, 0, len - 1, "utf-8");
47         } else {
48             return new String(buf, 0, len - 2, "utf-8");
49         }
50     }
51 
52     // 使用GBK编码表进行字符串的截取,一个英文字符对应码表一个正数,一个汉字对应两个负数
53     public static String cutStringByByte(String str, int len)
54             throws IOException {
55         byte[] buf = str.getBytes("gbk");
56         int count = 0;
57         for (int x = len - 1; x >= 0; x--) {
58             if (buf[x] < 0) {
59                 count++;
60             } else {
61                 break;
62             }
63         }
64         if (count % 2 == 0) {
65             return new String(buf, 0, len, "gbk");
66         } else {
67             return new String(buf, 0, len - 1, "gbk");
68         }
69     }
70 
71 }