字符串截取

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

package cn.hucn.IO.Day4;

import java.io.UnsupportedEncodingException;

public class StringCut {
    private StringCut() {
    }

    public static String cutStringByte(String str, int len) {
        if (System.getProperty("file.encoding").equalsIgnoreCase("gbk")) {
            return CutStringBGK(str, len);
        }
        if (System.getProperty("file.encoding").equalsIgnoreCase("utf-8")) {
            return CutStringUTF8(str, len);
        }
        return "当前系统不支持中文";
    }

    public static String CutStringBGK(String str, int len) {//GBK汉字两个负数码值
        try {
            byte buf[] = str.getBytes("gbk");
            int count = 0;
            for (int i = len - 1; i >= 0; i--) {
                if (buf[i] < 0) {
                    count++;
                } else {
                    break;
                }
            }
            if (count % 2 == 0) {
                return new String(buf, 0, len, "gbk");
            } else {
                return new String(buf, 0, len - 1, "gbk");
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("字符编码异常,该字符串包含非gbk字符", e);
        }

    }

    public static String CutStringUTF8(String str, int len) {//utf-8 汉字三个负数码值
        try {
            byte buf[] = str.getBytes("utf-8");
            int count = 0;
            for (int i = len - 1; i >= 0; i--) {
                if (buf[i] < 0) {
                    count++;
                } else {
                    break;
                }
            }
            if (count % 3 == 0) {
                return new String(buf, 0, len, "utf-8");
            } else {
                return new String(buf, 0, len-count%3, "utf-8");
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("字符编码异常,该字符串包含非utf-8字符", e);
        }
    }

    public static void main(String[] args) {
        String s = "ab你好";//汉字“琲”的编码是一负一正,不是一般汉字的两个负数码值
        byte buf[] = null;
        try {
            buf = s.getBytes("gbk");
             //buf = s.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        for (byte b : buf) {
            System.out.print(b + " ");
        }
        // 单元测试
        System.out.println();
        for (int i = 1; i <= buf.length; i++) {
            String ss = CutStringBGK(s, i);
            // String ss = CutStringUTF8(s, i);
            System.out.println(i + ":" + ss);
        }
    }
}

String s = “ab你好” GBK
97 98 -60 -29 -70 -61
1:a
2:ab
3:ab
4:ab你
5:ab你
6:ab你好

String s = “ab你好” UTF-8
97 98 -28 -67 -96 -27 -91 -67
1:a
2:ab
3:ab
4:ab
5:ab你
6:ab你
7:ab你
8:ab你好

String s = “ab你好琲”; UTF-8(“琲”三个负码值)
97 98 -28 -67 -96 -27 -91 -67 -25 -112 -78
1:a
2:ab
3:ab
4:ab
5:ab你
6:ab你
7:ab你
8:ab你好
9:ab你好
10:ab你好
11:ab你好琲

String s = “ab你好琲”; GBK(“琲”是一负一正码值)
97 98 -60 -29 -70 -61 -84 105
1:a
2:ab
3:ab
4:ab你
5:ab你
6:ab你好
7:ab你好
8:ab你好琲

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值