首页 > 其他 > 详细

字节数截取字符串

时间:2017-01-07 19:26:55      阅读:203      评论:0      收藏:0      [点我收藏+]
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);
    }
}

 

字节数截取字符串

原文:http://www.cnblogs.com/LZL-student/p/6260022.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!