字符截取题目

题目
在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。
但对应的字节数不同,一个汉字占两个字节。
定义一个方法,按照指定的字节数来取子串。
如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,那么半个就要舍弃。
如果取四个字节就是“ab你”,取五个字节还是“ab你”。
仅考虑GBK和utf-8编码

import java.io.UnsupportedEncodingException;

import org.junit.Test;

/**
 * @author<a href="mailto:953801304@qq.com">胡龙华</a>
 * @version 2017-4-4  下午1:08:45
 * @fileName StringCut.java
 */
public class StringCut {

    @Test
    public void analyze(){
        String str1 = "你好abc";
        byte[] bs1=null;
        byte[] bs2=null;
        try {
             bs1 = str1.getBytes("GBK");
             System.out.println("---GBK---");
             for(byte b:bs1){
                 System.out.print(b+" ");
             }
             System.out.println();
            //-60 -29 -70 -61 97 98 99 
            // 发现规律,再gbk中一个中文汉字 都是以两个字节  小于0的数存储
             bs2 = str1.getBytes("utf-8");
             System.out.println("---utf-8---");
             for(byte b:bs2){
                 System.out.print(b+" ");
             }
            //-28 -67 -96 -27 -91 -67 97 98 99 
            // 发现规律,在utf-8中一个中文汉字 是以三个字节 小于0 的数存储
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    /**
     * 思路:从第len个往前数,连续2的倍数个负数则全部输出,单数个则去掉最后一个输出
     * @param str
     * @param len
     * @return
     */
    private static String StringCutByGBK(String str,int len){
        byte[] bs = null;
        try {
            int count = 0;
            bs = str .getBytes("GBK");
            for(int i=len-1;i>=0;i--){
                if(bs[i]<0){
                    count++;
                }else{
                    break;
                }
                // 0   1   2   3   4  5   6  7   8   9   10  11  12    
            }   //-60 -29 -70 -61 -80 -95 97 98  99 -76 -17 -72 -25 
            if(count%2==0){
                String s=new String(bs, 0, len, "GBK");
                System.out.println("截取"+len+"个字符:"+s);
            }else{
                String s=new String(bs, 0, len-1, "GBK");
                System.out.println("截取"+len+"个字符:"+s);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * 思路:从第len个往前数,连续3的倍数个负数则全部输出,其他情况则去掉最后count%3个输出
     * @param str
     * @param len
     * @return
     */
    private static String StringCutByUTF8(String str,int len){
        byte[] bs = null;
        try {
            int count = 0;
            bs = str .getBytes("UTF-8");
            for(int i=len-1;i>=0;i--){
                if(bs[i]<0){
                    count++;
                }else{
                    break;
                }
            }   
            // 0   1   2   3   4   5   6  7  8  9   10  11  12
            //-60 -29 -70 -61 -80 -95 97 98 99 -76 -17 -72 -25 
            if(count%3==0){
                String s=new String(bs, 0, len, "UTF-8");
                System.out.println("截取"+len+"个字符:"+s);
            }else{
                String s=new String(bs, 0, len-count%3, "UTF-8");
                System.out.println("截取"+len+"个字符:"+s);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }
    @Test
    public void TEST() {
        String str = "你好啊abc达哥";
        try {
            System.out.println("---测试gbk---");
            byte bs  []  = str.getBytes("GBK");
            for(int i=0;i<=bs.length;i++){
                //System.out.print(bs[i]+" ");
                StringCutByGBK(str,i);

            }

            System.out.println("---测试UTF-8---");
            byte bs2  []  = str.getBytes("utf-8");
            for(int i=0;i<=bs2.length;i++){
                //System.out.print(bs[i]+" ");
                StringCutByUTF8(str,i);

            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值