博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java按字节截取字符串(GBK编码、UTF-8编码实现)
阅读量:6983 次
发布时间:2019-06-27

本文共 2155 字,大约阅读时间需要 7 分钟。

1 package FileDemo; 2  3 import java.io.IOException; 4  5 public class CutStringTest { 6  7     /** 8      * @param args 9      * @throws IOException10      */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 }

 

转载地址:http://ojtpl.baihongyu.com/

你可能感兴趣的文章
阿里云Redis (安装包安装篇)
查看>>
Spring Bean的装配(非XML文件方式)
查看>>
100万“愤怒的小鸟”:中国手机开发者生存调查
查看>>
centos 6.3 x86_64安装32位JDK的问题
查看>>
springmvc学习笔记(17)-上传图片
查看>>
驰骋工作流引擎表单设计控件-字段类控件(2)
查看>>
java高级-多线程编程
查看>>
spark内核回顾思考 RDD
查看>>
导出Android手机应用apk
查看>>
高并发分布式系统中生成全局唯一(订单号)Id
查看>>
在 iOS 应用中直接跳转到 AppStore 的方法
查看>>
IOS 委托和协议区别和联系 (-)
查看>>
通过JDBC连接取得数据库相关的元数据
查看>>
CentOS常用到的查看系统命令
查看>>
Validform使用入门
查看>>
Jfinal碰到的问题记录
查看>>
Kettle7 java 远程执行Trans/Job
查看>>
Python Socket 编程
查看>>
jenkins ssh发布配置
查看>>
Guava新集合-Multiset
查看>>