黑马程序员技术交流社区

标题: 怎么判断截取的部分包含中文 [打印本页]

作者: 1059232202    时间: 2014-4-15 10:51
标题: 怎么判断截取的部分包含中文
编写函数,从一个字符串中按字节数截取一部分,但不能截取出半个中文(GBK码表)
*例如:从“HM程序员”中截取2个字节是“HM”,截取4个则是“HM程”,截取3个字节也要是"HM"而不要出现半个中文
作者: 491138002    时间: 2014-4-15 12:54
本帖最后由 491138002 于 2014-4-15 12:56 编辑
  1. public class SplitString {

  2.     public static String bSubstring(String s, int length) throws Exception {

  3.         byte[] bytes = s.getBytes("Unicode");
  4.         int n = 0; // 表示当前的字节数
  5.         int i = 2; // 要截取的字节数,从第3个字节开始
  6.         for (; i < bytes.length && n < length; i++) {

  7.             // 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节

  8.             if (i % 2 == 1) {
  9.                 n++; // 在UCS2第二个字节时n加1
  10.             } else {
  11.                 // 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节
  12.                 if (bytes[i] != 0) {
  13.                     n++;
  14.                 }
  15.             }
  16.         }

  17.         // 如果i为奇数时,处理成偶数

  18.         if (i % 2 == 1) {
  19.             // 该UCS2字符是汉字时,去掉这个截一半的汉字
  20.             if (bytes[i - 1] != 0)
  21.                 i = i - 1;
  22.             // 该UCS2字符是字母或数字,则保留该字符
  23.             else
  24.                 i = i + 1;
  25.         }

  26.         return new String(bytes, 0, i, "Unicode");

  27.     }

  28.     public static void main(String[] args) throws Exception {
  29.         String s = "HM程序员";
  30.         String ss = SplitString.bSubstring(s, 3);
  31.         System.out.println(ss);
  32.     }
  33. }
复制代码

作者: 1059232202    时间: 2014-4-17 13:28
Unicode编码是一个字符占两个字节!3、5、7貌似是对应US2中的第一个字节吧?
作者: luis    时间: 2014-4-17 15:16
同求,希望有高人出招
作者: jiangshicun007    时间: 2015-11-29 13:14
package Thread05;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class test1 {

        /**
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                String st="HM程序员sn我";
                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("yyy.txt"));
                bos.write(st.getBytes());//把字符串写入到yyy.txt
                bos.flush();//刷新缓存
                BufferedInputStream bs=new BufferedInputStream(new FileInputStream("yyy.txt"));
                int leng=st.length();//字符串的字符个数
                int byteleng=bs.available();//字符串的字节长度
                int num=byteleng-leng;//汉字个数
                int getnum=9;//需要取的字节个数
                int sum=0;
                for (int i = 0; i < st.length(); i++) {
                        String newst=st.charAt(i)+"";
                        BufferedOutputStream bos1=new BufferedOutputStream(new FileOutputStream("zzz.txt"));
                        bos1.write(newst.getBytes());
                        bos1.flush();
                        BufferedInputStream bs1=new BufferedInputStream(new FileInputStream("zzz.txt"));
                        sum=sum+bs1.available();
                        if(sum>getnum){
                                String newstring=st.substring(0, i);
                                System.out.println(newstring);
                                break;
                        }
                       
                }
               
        }

}
搞了好久才搞出来!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2