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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 齐银春 中级黑马   /  2012-11-24 18:39  /  1527 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

虽说最后弄出来了 可我再次用gbk解码编码 当截取六个字节时总是无法截取完整  请大家帮忙看一下有什么错误
import java.io.UnsupportedEncodingException;

public class Test10 {

        /**
         * 10、 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
         * 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,
         * 6,应该输出为“我ABC”而不是“我ABC+汉的半个”
         *
         * @param args
         * @throws Exception
         */
        private static String cutString(String str,int index) throws Exception{
                byte[] buf=str.getBytes("gbk");
                int count=0;
                for(int x=index;x>=0;x--){
                        if(buf[x]<0)
                                count++;
                        else
                                break;
                }
                if(count%2==0)
                        return new String(buf,0,index,"gbk");
                else
                        return new String(buf,0,index-1,"gbk");
                       
               
        }
        public static void main(String[] args) throws Exception {
                String str="我ABC汉DEF";
                String str1=cutString(str,6);
                System.out.println(str1);

        }

}


评分

参与人数 1技术分 +1 收起 理由
杨千里 + 1

查看全部评分

2 个回复

倒序浏览
我以前写过的、你看看吧,也许有用
import java.util.Scanner;
/*
编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
但是要保证汉字不被截取半个,如“我ABC”4,应该截取为“我AB”,输入“我ABC汗DEF”,6,应该输出为“我ABC”,而不是“我ABC+汗的半个字”。
*/
public class JieQu
{
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
                System.out.println("输入要截取的字符:");
                String str =sc.nextLine();
                System.out.println("输入要截取的长度:");
                int num = sc.nextInt();
                Substring substr = new Substring(str,num);
                substr.splitit();
        }
}
class Substring
{
        private String str;//要截取的字符
        private int bytenum;//截取个数
        public Substring(){}
    public Substring(String str,int bytenum){
            this.str = str;
                this.bytenum = bytenum;
        }
        public void splitit(){
             byte[] bt = str.getBytes();
                 System.out.println("字符串的长度为:"+bt.length);
                 if(bytenum>1){
                       if(bt[bytenum]<0){
                     //调用String方法的默认字符集解码byte 子数组,构造一个新的 String
                                 String substrx = new String(bt,0,--bytenum);
                                                 System.out.println(substrx);
                           }
                           else{
                                  String substrex = new String(bt,0,bytenum);
                      System.out.println(substrex);
                           }
                 }
                 else{
                         if(bytenum==1){

                                           if(bt[bytenum]<0)
                                                        {
                                                                String substr1=new String(bt,0,++bytenum);
                                                                System.out.println(substr1);
                                                        }
                                                        else
                                                        {
                                                                String substr2=new String(bt,0,bytenum);
                                                                System.out.println(substr2);
                                                        }
                                        }
                                        else
                                        {
                                                System.out.println("输入错误!!!请输入大于零的整数:");
                                        }
                          }
           }
}

评分

参与人数 1技术分 +1 收起 理由
杨千里 + 1

查看全部评分

回复 使用道具 举报
潘天功 发表于 2012-11-24 20:19
我以前写过的、你看看吧,也许有用
import java.util.Scanner;
/*

这个我用utf-8正常 但gbk有时候会出错  你的也应该是默认gbk编码的吧
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马