字节数截取字符串

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);
    }
}
原文地址:https://www.cnblogs.com/LZL-student/p/6260022.html