//截取中文
public static String subString(String str, int count) {
byte[] temp = str.getBytes();
byte[] bArray = new byte[count * 2];
int i;
int ii = 0;// 用于判断最后一个是不是一半汉字
String strc = "full";
for (i = 0; i < count; i++) {
bArray[i] = temp[i];
}
for (i = 0; i < count; i++) {
if (bArray[i] < 0) {
ii++;
}
}
if (ii % 2 != 0) {
strc = "hard";
}
// 截下去为完全的时候
if (strc.equals("full") && bArray[i] < 0) {
bArray[i] = ' ';
}
// 截下去为一半的时候
if (strc.equals("hard") && bArray[i - 1] < 0) {
bArray[i - 1] = ' ';
}
// System.out.println(new String(bArray).trim());
return new String(bArray).trim();
}
如:
String str = "取前面七个字符";
System.out.println(subString(str,7));//取前面