**编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字 节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入" 我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。 **
package demo06;
public class Test5 {
public static void main(String[] args) {
Cut(" 我ABC汉DEF",3);
}
public static void Cut(String s,int num) {
int count=0;
String temp="";
for(char a:s.toCharArray()) {
count+=(a+"").getBytes().length;
if (count>num) {
break;
}
temp+=a;
}
System.out.println(temp);
}
}