字节数截取字符串

Posted 卡拉瓦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字节数截取字符串相关的知识,希望对你有一定的参考价值。

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);
    }
}

 

以上是关于字节数截取字符串的主要内容,如果未能解决你的问题,请参考以下文章