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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/*
第10题:编写函数,从一个字符串中按字节数截取一部分,但不能截取出半个中文(GBK码表)
例如:从“HM程序员”中截取2个字节是“HM”,截取4个则是“HM程”,截取3个字节也要是"HM"而不要出现半个中文

分析:
1、首先将字符串转换成字节数组,可以自定义获取字节个数进行字节的获取
2、按照要求不能获取半个中文,因为在GBK编码表中对应的汉字是用复数表示的,所以可以根据获取到最后一个字节在GBK中的编码是否小于0进行判断,
如果小于0,则将最后一个字节忽略,进行输出
拓展思路:
结合集合、流 进行拓展练习
1、从键盘录入获取字符串以及要截取的字符个数,以输入over结束输入,
2、对获取的信息封装成Informations对象,存储进List集合中
3、调用方法对字符串进行截取,判断如果最后一个字节为负数且倒数第二个字符不为负数,则最后一个字节为半个中文,这时只截取数-1,
截取到的字节就能转成有效的字符串,而不会出现乱码的情况;
4、将截取后的字符串也存储进集合,通过遍历集合的输出最终的截取结果
*/

  1. import java.io.*;
  2. import java.util.*;
  3. //定义一个Informations类,用来将获取到的键盘输入信息封装成对象
  4. class Informations
  5. {
  6.         private String str;
  7.         private int len;
  8.         Informations(String str,int len)
  9.         {
  10.                 this.str=str;
  11.                 this.len=len;
  12.         }
  13.         public String getString()
  14.         {
  15.                 return this.str;
  16.         }
  17.         public int getLen()
  18.         {
  19.                 return this.len;
  20.         }
  21.         public String toString()
  22.         {
  23.                 return this.str+","+(this.len+"");
  24.        
  25.         }

  26. }
  27. class Test10
  28. {
  29.         public static void main(String[] args)
  30.         {
  31.                 System.out.println("请按照 \"字符串,截取字节个数\" 的形式输入信息,输入over结束:");
  32.                 getInfoAndGetParts();//调用字符串截取字节方法
  33.         }
  34.        
  35.         //定义一个截取键盘录入信息,实现字节截取的方法
  36.         public static void getInfoAndGetParts()
  37.         {
  38.             //通过键盘录入获取字符串
  39.                 BufferedReader bufr=null;
  40.                
  41.                 try
  42.                 {
  43.                         List<Informations> infoList= new ArrayList<Informations>();//定义一个集合来存储键盘输入的信息
  44.                     Informations inf=null;
  45.                         bufr=new BufferedReader(new InputStreamReader(System.in));                       
  46.                         String line=null;
  47.                         while((line=bufr.readLine())!=null)
  48.                         {
  49.                                 if("over".equals(line))//定义键盘输入以over结束
  50.                                          break;
  51.                            else
  52.                                 {
  53.                                         String[] in=line.split(",");
  54.                                         inf=new Informations(in[0],Integer.parseInt(in[1]));
  55.                                         infoList.add(inf);//将Informations对象存储进List列表
  56.                                 }
  57.                                
  58.                         }
  59.                        
  60.                         List<String> st=getParts(infoList);//对列表中的信息进行字节截取
  61.                         for(String ss:st)//对集合进行遍历
  62.                         {
  63.                                 System.out.println("截取字节后的有效字符串是:"+ss);               

  64.                         }
  65.                        
  66.                        
  67.                 }
  68.                 catch (IOException ioe)
  69.                 {
  70.                         System.out.println("截取字符串失败;");
  71.                 }
  72.                 finally
  73.                 {
  74.                         try
  75.                         {
  76.                                 if(bufr!=null)
  77.                                    bufr.close();
  78.                         }
  79.                         catch (IOException ioe)
  80.                         {
  81.                                 System.out.println("关闭流失败");
  82.                         }
  83.                
  84.                 }

  85.         }
  86. /*定义一个获取制定字节数的方法,接收一个存储信息的数组,一个字符串和截取个数;
  87.         通过遍历Informations集合,获取字符串和截取字节个数,进行截取操作;
  88.         再将截取后的有效字符串存储进List集合中
  89.        
  90.         */
  91.         public static List<String> getParts(List<Informations> list)
  92.         {
  93.                 Iterator<Informations> it=null;
  94.                 List<String> partList=new ArrayList<String>();//定义一个List集合用来存储截取后的字符串
  95.                 byte[] by={};//定义一个数组,存储字符串编码成字节后的数据
  96.                 String str=null;
  97.                 int len=0;
  98.                 try
  99.                 {
  100.                         for(Informations infos:list)
  101.                         {
  102.                                 str=infos.getString();//获取集合中存储的Informations对象的字符串
  103.                                 by=str.getBytes("GBK");//将获取到的集合中的字符串根据GBK编码表转换成字节存如数组中
  104.                                 len=infos.getLen();//获取集合中存储的Informations对象的截取个数值
  105.                                 if(len>by.length)//如果要截取的字节个数超出了字符串范围,返回提示语句
  106.                                         System.out.println("截取长度超出字符串范围");
  107.                                 if(by[len-1]<0 & by[len-2]>0)//如果截取的最后一个字节小于0,并且截取的倒数第二个字节大于0,将截取个数-1,相当于只截取前面的有效字节
  108.                                 {
  109.                                         len=len-1;
  110.                                 }
  111.                        
  112.                            partList.add(new String(by,0,len,"GBK"));
  113.                                
  114.                         }
  115.                        
  116.                 }
  117.                 catch (UnsupportedEncodingException ue)
  118.                 {
  119.                         System.out.println("编码转换失败");
  120.                 }
  121.                
  122.                 return partList;
  123.        
  124.         }

  125. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马