看了http://xifeng91.iteye.com/blog/268589的代码,觉的可以让代码更精简些,如下:
public static String bSubstring(String s, int length) throws Exception {
byte[] bytes = s.getBytes("Unicode");
int i=0,n=0;
while(n<length&&i<bytes.length){
i+=2;
//判断是否为双字节字符
if(bytes[i]==0){
n+=1;
}else{
n+=2;
}
}
return new String(bytes,0,i,"Unicode");
}