A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Abstact小哲 中级黑马   /  2013-9-10 19:40  /  1072 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Abstact小哲 于 2013-9-11 11:13 编辑

编写函数,从一个字符串中按字节数截取一部分,但不能截取出半个中文(GBK码表)

// 例如:从“HM程序员”中截取2个字节是“HM”,截取4个则是“HM程”,截取3个字节也要是"HM"而不要出现半个中文

这个题没思路啊。。不知道怎么做, 哪位朋友能帮忙指点1下
先谢谢了~

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

4 个回复

倒序浏览
  1. public static String splitString(String str, int len) {
  2.                 if (str == null && "".equals(str)) { // 字符串是否为空
  3.                         return null;
  4.                 }
  5.                 byte[] strBytes = null;
  6.                 try {
  7.                         strBytes = str.getBytes("GBK");
  8.                 } catch (Exception e) {
  9.                         e.printStackTrace();
  10.                 }

  11.                 int strLen = strBytes.length;// 字符串的长度

  12.                 if (len >= strLen || len < 1) {
  13.                         return str;
  14.                 }
  15.                 int count = 0;
  16.                 for (int i = 0; i < len; i++) {

  17.                         int value = strBytes[i];

  18.                         if (value < 0) {
  19.                                 count++;
  20.                         }
  21.                 }

  22.                 if (count % 2 != 0) {

  23.                         len = (len == 1) ? len : len - count / 2 - 1;
  24.                 } else {

  25.                         len = len - (count / 2);
  26.                 }
  27.                 return str.substring(0, len);
  28.         }
复制代码
只提供参考。。不要抄袭

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

回复 使用道具 举报
说一下思路:
1、首先把源字符串转化为字节数组。
str=“HM程序员”
byte[]   origin= str.getBytes("GBK");
2、从源字符数组取出给定的字节数作为新子串。
新子串的为newStr= new String(origin, 0, 3);
2、通过比较新子串和源字符串相同位置上的字符是否相等来判断新子串中的字节是否是汉字。
假设新子串为取三个字节,为new= new String(origin, 0, 3),把新子串和源字符串相同位置上的字符进行比较,当比较到第三个字符是,源字符串的字符为”程“,而新子串上的字符为”程“的第一个字节不想等,所以就舍弃。

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

回复 使用道具 举报
public class Demo {       
        public static void main(String[] args) throws Exception{
        String str ="HM程序员";
        int num =trimGBK(str.getBytes("GBK"),4); // HM程
        System.out.println(str.substring(0,num));
    }      
    public static int trimGBK(byte[] buf,int n){
        int num = 0;
        Boolean  bChineseFirstHalf = false;
        for(int i=0;i<n;i++){
            if(buf[i]<0 && !bChineseFirstHalf){
                bChineseFirstHalf= true;
            }else{
                num++;
                bChineseFirstHalf= false;                        
            }
        }
        return num;
   }
}

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

回复 使用道具 举报
  1. public class Test10 {

  2.         public static void main(String[] args) throws UnsupportedEncodingException {
  3.                 Scanner sc = new Scanner(System.in);// 创建一个Scanner对象
  4.                 System.out.println("请输入字符串");
  5.                 String s = sc.nextLine();// 输入字符串参数
  6.                 System.out.println("请输入字节数");
  7.                 int num = sc.nextInt();// 输入字节数参数
  8.                 sc.close();// 关闭流
  9.                 String s1 = splitDemo(s, num);// 将参数传入调用的方法
  10.                 System.out.println("切割后的字符串是:" + s1);// 对截取结果进行打印
  11.         }

  12.         public static String splitDemo(String str, int num)
  13.                         throws UnsupportedEncodingException {

  14.                 if (str == null) {// 当字符串被赋值为null时的情况
  15.                         return "你的字符串没有值";
  16.                 }

  17.                 if ("".equals(str)) {// 当字符串长度为零的情况
  18.                         return "你输入的是空的";
  19.                 }

  20.                 int count = 0;// 定义一个数来记录小于零的字节数用来求有多少汉字
  21.                 byte[] arr = str.getBytes("GB2312");// 把传入的字符串通过getbyte(String str)方法得到的一个GB2312编码的字节数组arr

  22.                 for (int i = 0; i < num; i++) {// 遍历0到num这段字节数组求出负数的个数
  23.                         if (arr[i] < 0) {
  24.                                 count++;// 求出count的值
  25.                         }
  26.                 }

  27.                 if (count % 2 == 0) {// 负字节数是偶数的情况
  28.                         return new String(arr, 0, num, "GB2312");
  29.                 }

  30.                 if (count % 2 == 1) {// 负数字节数是基数的情况
  31.                         return new String(arr, 0, num + 1, "GB2312");
  32.                 }
  33.                
  34.                 return null;
复制代码
这个是我自己做的,那个GBK的编码的汉字对应的字节数值不一定是负数,所以我采用的是GB2312编码表来写的,希望能解决你的问题。

  名称  
第一字节
第二字节
GB2312
0xB0-0xF7(176-247)
0xA0-0xFE160-254
GBK
0x81-0xFE129-254
0x40-0xFE64-254
这是GBk和GB2312编码表的区别,希望能帮到你!

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马