已经测试通过
public class SplitIt {
public static void main(String[] args) {
String str = "我ABC汉DEF";
int a = 6;
excute(str,a);
}
private static void excute(String str, int a) {
String temp = "";
int k = 0;
for (int i = 0; i < str.length(); i++) {
byte[] b = (str.charAt(i) + "").getBytes(); //每循环一次,将str里的值放入byte数组
k = k + b.length;
if (k > a) { //如果数组长度大于6,随机跳出循环
break;
}
temp = temp + str.charAt(i); //拼接新字符串
}
System.out.println(temp);
}
} |