按照字节长度截取string:
记录一下
public class Stri {
public static void main(String args[]) throws UnsupportedEncodingException {
String sr="我a中guo号";
//中文字节长度 gbk 2 utf-8 3
byte [] be=sr.getBytes("gbk");
System.out.println(getStrIndex(be,10));
System.out.println(new String(be, 0, getStrIndex(be,10),"gbk"));
}
public static int getStrIndex(byte [] be,int i){
//截取字节长度必须小于字符串字节长度
if(i<be.length) {
//asi小于0 则递归
if (be[i] < 0) {
i--;
if (i > 0) {
getStrIndex(be, i);
}
}
}
return i;
}
}