Java - USC2字符串截取

Java内部采用UTF-16(USC2)编码,比如:
"我" 为 98 17,
"a" 为 0 97,
" " 为 0 32,
"1" 为 0 49....

 

 

 

public static String cutString(String s, int length) throws Exception{

      byte[] bytes = s.getBytes("Unicode");

        int n = 0;

        int i = 2;//标志两位 [0]=-2 [1]=-1

        for (; i < bytes.length && n < length; i++) {

            if (i % 2 == 1) {

                n++;

            } else {

                if (bytes[i] != 0) {

                    n++;

                }

            }

        }

        if (i % 2 == 1) {

            if (bytes[i - 1] != 0)

                i = i - 1;

            else

                i = i + 1;

        }

        return new String(bytes, 0, i, "Unicode");

    }

你可能感兴趣的:(字符串截取)