中英文混排的字串拆分

來源:互聯網
上載者:User

1 UTF-8編碼

/**
 * 把字串以UTF-8編碼按照佔用指定的位元組數進行拆分,支援中英文混排
 * 
 * @param wideStr
 *            要拆分的字串
 * @param maxByteLen
 *            字串佔用的最大位元組數
 * @return 如果不需要拆分,返回null。否著返回拆分好的字串列表。
 */
public List<String> splitUTF8Words(String wideStr, int maxByteLen) {
    if (maxByteLen < 3 || wideStr.length() < (maxByteLen / 3)) {
        return null;
    } else {
        try {
            byte[] bytes = wideStr.getBytes("utf-8");
            int bytesLen = bytes.length;
            if (bytesLen < maxByteLen) {
                return null;
            }
            List<String> result = new ArrayList<String>(3);
            int i = 0; // 指向原數組的標記。
            int count = 0; // 該組的計數器
            int currSplitStrStartIndex = 0; // 當前拆分的字元起始位置
            int currSplitStrIndex = -1; // 當前拆分時,計算的字元
            int lastIndex = -1; // 為了回退
            while (i < bytesLen) {
                byte b = bytes[i]; // 起始字元
                lastIndex = i;
                if ((b & 0x80) == 0) {
                    i++;
                    count++;
                } else if ((b & 0xe0) != 0) {
                    // 3個位元組的漢字
                    i = i + 3;
                    count = count + 3;
                } else {
                    // 2個位元組
                    i = i + 2;
                    count = count + 2;
                }
                currSplitStrIndex++;
                if (count == maxByteLen) {
                    result.add(wideStr.substring(currSplitStrStartIndex,
                            currSplitStrIndex + 1));
                    // 清零
                    count = 0;
                    currSplitStrStartIndex = currSplitStrIndex + 1;
                } else if (count > maxByteLen) {
                    // 如果大於需要回退
                    i = lastIndex;
                    currSplitStrIndex--;
                    result.add(wideStr.substring(currSplitStrStartIndex,
                            currSplitStrIndex + 1));
                    // 清零
                    count = 0;
                    currSplitStrStartIndex = currSplitStrIndex + 1;
                }
            }
            if (currSplitStrIndex >= currSplitStrStartIndex) {
                result.add(wideStr.substring(currSplitStrStartIndex,
                        currSplitStrIndex + 1));
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

 

2 GBK編碼,原理差不多

 

/**
 * 把字串以GBK編碼按照佔用指定的位元組數進行拆分,支援中英文混排
 * 
 * @param wideStr
 *            要拆分的字串
 * @param maxByteLen
 *            字串佔用的最大位元組數
 * @return 如果不需要拆分,返回null。否著返回拆分好的字串列表。
 */
public List<String> splitGBKWords(String wideStr, int maxByteLen) {
    if (maxByteLen < 2 || wideStr.length() < (maxByteLen / 2)) {
        return null;
    } else {
        try {
            byte[] bytes = wideStr.getBytes("gbk");
            int bytesLen = bytes.length;
            if (bytesLen < maxByteLen) {
                return null;
            }
            List<String> result = new ArrayList<String>(3);
            int i = 0; // 指向原數組的標記。
            int count = 0; // 該組的計數器
            int currSplitStrStartIndex = 0; // 當前拆分的字元起始位置
            int currSplitStrIndex = -1; // 當前拆分時,計算的字元
            int lastIndex = -1; // 為了回退
            while (i < bytesLen) {
                byte b = bytes[i]; // 起始字元
                lastIndex = i;
                if ((b & 0x80) == 0) {
                    // 首位為0說明是英文
                    i++;
                    count++;
                } else {
                    // 漢字
                    i = i + 2;
                    count = count + 2;
                }
                currSplitStrIndex++;
                if (count == maxByteLen) {
                    result.add(wideStr.substring(currSplitStrStartIndex,
                            currSplitStrIndex + 1));
                    // 清零
                    count = 0;
                    currSplitStrStartIndex = currSplitStrIndex + 1;
                } else if (count > maxByteLen) {
                    // 如果大於需要回退
                    i = lastIndex;
                    currSplitStrIndex--;
                    result.add(wideStr.substring(currSplitStrStartIndex,
                            currSplitStrIndex + 1));
                    // 清零
                    count = 0;
                    currSplitStrStartIndex = currSplitStrIndex + 1;
                }
            }
            if (currSplitStrIndex >= currSplitStrStartIndex) {
                result.add(wideStr.substring(currSplitStrStartIndex,
                        currSplitStrIndex + 1));
            }
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.