淺析Oracle varchar2類型及根據字符集獲取在Java中的字符字節/截取工具類

   首先varchar2(byte)最大是4000字節。

   varchar2(50)  表示該字段類型爲varchar2類型,長度爲50,可以存單字節字符50個。

    長度跟你的字符集和其編碼都有關係:

  如果字符集是16位編碼的,ZHS16GBK(其實就是GBK,),那麼每個字符16位、2字節,所以可以容納2000字符。

  如果是32位編碼的字符集,那麼只能存儲 1000個字符。

  如何查看自己數據庫字符集和編碼呢?

select userenv('language') from dual;

 分享一個獲取Java中字符串類型字節長度和根據字節截取的工具類

 

/**
     * 按字節長度截取字符串
     *
     * @param orgin       需要截取的字符串
     * @param blength     需要保留的字節長度
     * @param charsetName 編碼,對於字符集爲UTF-8的數據庫,請指定編碼爲UTF-8;字符集爲GBK的數據庫,請指定編碼GBK
     * @return 截取後的字符串
     * @throws UnsupportedEncodingException 不支持的字符編碼
     */
    public static String subString4Byte(String orgin, int blength, String charsetName) {
        return subStringb2(orgin, blength, charsetName).get(0);
    }

    /**
     * @Auther: liuzujie
     * @Date: 2020/1/16 18:19
     * @Desc: 獲取字符串字節數
     * @param: orgin(字符串)charsetName(字符集)
     */
    public static final int getStringByteLength(String orgin, String charsetName) {
        byte[] bs = new byte[0];
        try {
            bs = orgin.getBytes(charsetName);
        } catch (UnsupportedEncodingException e) {
            log.error("獲取字符串字節數異常,orgin:{},charsetName:{},info:", orgin, charsetName, e);
        }
        return bs.length;
    }
    
    private static List<String> subStringb2(String orgin, int blength, String charsetName) {
        List<String> result = new ArrayList<>();
        int length;
        byte[] bs = new byte[0];
        try {
            bs = orgin.getBytes(charsetName);
            while (bs.length > 0) {
                length = blength;
                if (length >= bs.length) {
                    result.add(new String(bs, 0, bs.length, charsetName));
                    break;
                }
                if ("UTF8".equals(charsetName.toUpperCase()) || "UTF-8".equals(charsetName.toUpperCase())) {
                    while (length > 0) {
                        if ((bs[length] | 0x7F) == 0x7F) {
                            break;
                        }
                        if ((bs[length] & 0xC0) == 0xC0) {
                            break;
                        }
                        length--;
                    }
                } else if ("GBK".equals(charsetName.toUpperCase())) {
                    boolean removLast = length % 2 == 1;
                    for (int i = 0; i < length; i++) {
                        if ((bs[i] | 0x7F) == 0x7F) {
                            removLast = !removLast;
                        }
                    }
                    if (removLast) {
                        length--;
                    }
                } else if ("UNICODE".equals(charsetName.toUpperCase())) {
                    if (length % 2 == 1) {
                        length--;
                    }
                } else if ("UTF-16".equals(charsetName.toUpperCase()) || "UTF16".equals(charsetName.toUpperCase())) {
                    if (length % 2 == 1) {
                        length--;
                    }
                } else if ("UTF-16BE".equals(charsetName.toUpperCase())) {
                    if (length % 2 == 1) {
                        length--;
                    }
                } else if ("UTF-16LE".equals(charsetName.toUpperCase())) {
                    if (length % 2 == 1) {
                        length--;
                    }
                }
                result.add(new String(bs, 0, length, charsetName));
                bs = Arrays.copyOfRange(bs, length, bs.length);
            }
            if (result.size() == 0) {
                result.add("");
            }
        } catch (UnsupportedEncodingException e) {
            log.error("按字節長度分割字符串異常,orgin:{},blength:{},charsetName:{},info:", orgin, blength, charsetName, e);
        }
        return result;
    }

    public static void main(String[] args){
        String str="你。.3,3 2好2你223bdewj。";
        System.out.println(subString4Byte(str,9,"GBK"));
        System.out.println(getStringByteLength(str,"GBK"));
    }

 

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章