java基础知识回顾之---java String final类普通方法的应用之“按照字节截取字符串”

/*
需求:在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。
但对应的字节数不同,一个汉字占两个字节。
定义一个方法,按照最大的字节数来取子串。
如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,
那么半个就要舍弃。如果去四个字节就是“ab你”,取五个字节还是“ab你”.
*/

代码:其实是一个解码和编码的问题,要明白UTF-8码表和GBK码表的区别,UTF-8中用三个字节代表一个汉字,GBK使用2个字节代表一个汉字。

且在码表中都是用数字存放这些汉字。例如在GBK码表中,“你”为“-60 -29”;“好”为“-70 -61”,"谢"为"-48 -69".

我们把字符串先变成字节,按字节截取字符串。也就是先编码成某个码表中的数字,然后在把数字翻译过来,也就是查码表。对于GBK码表来说,两个数字代表一个汉字,且一般汉字用负数代表。那么我们可以来统计负数的个数count来决定是否舍弃半个汉字的问题,然后在判断负数的个数的奇偶性,如果count是奇数,则截取最后一个数字,舍弃半个汉字,解码输出,如果是偶数,全部解码输出。

public class Test {

    /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { String str = "ab你好cd谢谢";// 97 98 -60 -29 -70 -61 99 100 -48 -69 -48 -69 int len = str.getBytes("gbk").length;
for(int x=0; x<len; x++){
System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByByte(str, x+1));
} // int len = str.getBytes("utf-8").length;
// for(int x=0; x<len; x++){
// System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByU8Byte(str, x+1));
// } } /*        String str1 = "琲";//-84 105
        byte[] buf = str1.getBytes("gbk");
        for(byte b : buf){
             System.out.print(" "+b);//-84  105
*/
/**
* 使用Utf-8按照字节截取字符串,Utf-8用3个字节代表一个汉字。
*/
public static String cutStringByU8Byte(String str, int len) throws IOException { byte[] buf = str.getBytes("utf-8");//编码
System.out.println(buf); int count = 0;
for(int x=len-1; x>=0; x--){
if(buf[x]<0)
count++;
else
break;
} if(count%3==0)
return new String(buf,0,len,"utf-8");//解码
else if(count%3==1)
return new String(buf,0,len-1,"utf-8");
else
return new String(buf,0,len-2,"utf-8"); }
/**
* 使用字节截取字符串,gbk默认一个汉字为2个字节,编码表用两个数字代表一个汉字。
* (60 -29 -70 -61)代表你好
* 思路:我们可以记录负数的个数,
* 如果数字的个数为偶数,不用截取,没有半个汉字的情况,
* 如果负数的个数为奇数,那么加入有5个数字,那么最后一个数字舍弃。变为4个数字,再解码。
* @param str
* @param len
* @return
* @throws IOException
*/
public static String cutStringByByte(String str,int len) throws IOException{ byte[] buf = str.getBytes("gbk");//把指定编码的字符串转化为字节,存放到字节数组中,编码
int count = 0;
for(int x=len-1; x>=0; x--){//从数组的最后面开始循环,记录负数的个数
if(buf[x]<0)//汉字的编码表为负数
count++;
else
break;
} if(count%2==0)//负数的个数为偶数,不截取
return new String(buf,0,len,"gbk");//解码
else
return new String(buf,0,len-1,"gbk");//舍弃一位数字,解码
}
}
 
上一篇:EasyUI tree扩展获取实心节点


下一篇:Code reviews and bad habits