public class Test {
//要进行截取操作的字符串
static String ss;
//截取的字符串的字节数
static int n;
public static void main(String[] args) {
System.out.println("请输入字符串:");
//从键盘获取字符串
Scanner scStr = new Scanner(System.in);
//将Scanner对象中的内容以字符串的形式取出来
ss = scStr.next();
System.out.println("请输入字节数:");
//从键盘获取字符串
Scanner scByte = new Scanner(System.in);
//将Scanner对象中的内容以数值的形式取出来
n = scByte.nextInt();
//方法与方法间的套用
Interception(setValue());
}
//此方法的作用是将字符串转换成字符串数组
public static String[] setValue(){
//创建一个字符数组string
String[] string = new String[ss.length()];
for (int i = 0;i < string.length;i++){
//将字符串ss中的第i个字符取出,放入字符数组中string中
string[i] = ss.substring(i,i+1);
}
//将这个字符数组返回
return string;
}
public static void Interception(String[] string){
int count = 0;
String m = "[\\u4e00-\\u9fa5]";
System.out.println("每" + n + "字节进行划分的字符串如下所示:");
for (int i = 0; i < string.length;i++){
if (string[i].matches(m)){
count = count + 2;
}else {
count = count + 1;
}
if (count < n){
System.out.println(string[i]);
}else if (count == n){
System.out.println(string[i]);
count = 0;
System.out.println();
}else {
count = 0;
System.out.println();
}
}
}
}
04-18 05:20