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"); }
时间: 2024-10-06 05:26:16