打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JAVA 按字节截取字符串
    /**
     * 字符串按字节截取
     * @param str 原字符
     * @param len 截取长度
     * @param elide 省略符等追加到截取完的后边
     * @return String
     */
    public static String splitString(String str,int len,String elide) {
        if(str == null) {
            return "";
        }
        byte[] strByte = str.getBytes();
        int strLen = strByte.length;
        int elideLen = (elide.trim().length() == 0) ? 0 : elide.getBytes().length;
        if (len >= strLen || len < 1) {
            return str;
        }
        if (len - elideLen > 0) {
            len = len - elideLen;
        }
        int count = 0;
        for (int i = 0; i < len; i++) {
            int value = (int) strByte[i];
            if (value < 0) {
                count++;
            }
        }
        if (count%2 != 0) {
            len = (len == 1)?len+1:len - 1;
        }
        return new String(strByte, 0, len) + elide.trim();
    }     
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java 实现中英文混时得到长度 过长字符替换 - tianwaishan的专栏 - CS...
精确截取字符串
C++ 笔试 基础之 07 将字符串的前N个字符平移到字符串的后面
压缩软件(哈夫曼算法实现) 项目总结
Go语言实现Base64、Base58编码与解码
6种php上传图片重命名的实例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服