用java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"**
private static void Test2() throws UnsupportedEncodingException {
String str = “做A是111”;
int n = 7;
byte[] num = str.getBytes();
String tom1 = new String(num, 0, n);
String tom2 = new String(num, 0, n + 1);
String tom3 = new String(num, 0, n + 2);
int a = tom1.getBytes().length;
int b = tom2.getBytes().length;
int c = tom3.getBytes().length;
if (a == b && b == c) {
String tom4 = new String(num, 0, n - 1);
System.out.println(tom4);
}
if (a != b && b == c) {
String tom5 = new String(num, 0, n);
System.out.println(tom5);
}
if (a != b && b != c) {
String tom5 = new String(num, 0, n);
System.out.println(tom5);
}
if (a == b && b != c) {
String tom5 = new String(num, 0, n - 2);
System.out.println(tom5);
}
}