字节数截取字符串

public class ByteSub {

    public static void subString(String str, int a) {

        int bytes = 0;
        String result = "";
        for (int x = 0; x < str.length(); x++) {
            byte[] b = (str.charAt(x) + "").getBytes();
            bytes += b.length;
            if (bytes > a) {
                break;
            } else {
                result += str.charAt(x);
            }
        }
        System.out.println(result);
    }

    public static void main(String[] args) {
        subString("中国abcd", 3);
    }
}

 

posted @ 2017-01-07 18:44  卡拉瓦  阅读(291)  评论(0编辑  收藏  举报