电竞比分网-中国电竞赛事及体育赛事平台

分享

JAVA 按字節(jié)截取字符串

 躍來躍去 2009-12-15
    /**
     * 字符串按字節(jié)截取
     * @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();
    }     

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約