1.分割char数组,比如输入1,5,则将下标为1到5之间的字符返回。
import java.util.Scanner;
public class splitChar{
public static void main(String args[]){
int startid,endid;
char []arraychar={'a','c','d','v','x'};
System.out.println("请输入数组开始下标:");
Scanner s=new Scanner(System.in);
startid=s.nextInt();
System.out.println("请输入数组结束下标:");
Scanner e=new Scanner(System.in);
endid=e.nextInt();
System.out.println("分割后的字符数组为:");
if(startid>=0&&startid<=arraychar.length-1&&endid>=0&&endid<=arraychar.length-1&&startid<=endid){
for(int i=startid;i<=endid;i++){
System.out.print(arraychar[i]);
}
}else{
System.out.println("开始下标或结束输入下标有误,无法分割字符数组:");
}
}
}
2.char数组分割,如果以传入字符串为基准分割,如{'a','b','a','c','b','d'}分割成{'a'},{'a','c'},{'d'}三个char数组
import java.util.Scanner;
public class splitCharByletter{
public static void main(String args[]){
char splitc;
int j=0;
int flag=0;
int flaga=0;
char []arraychar={'d','c','d','1','x','d'};
int []splitcharA=new int[10];
int []splitchar;
System.out.println("请输入数组分割字符:");
Scanner s=new Scanner(System.in);
splitc=s.next().charAt(0);
for(int i=0;i<arraychar.length;i++){
if(arraychar[i]==splitc){
splitcharA[j]=i;
j++;
}
}
splitchar=new int[j];
for(int i=0;i<splitchar.length;i++){
splitchar[i]=splitcharA[i];
}
System.out.print("分割后数组如下:");
for(int i=0;i<splitchar[0];i++){
System.out.print(arraychar[i]);
}
if(splitchar.length>0){
for(int i=0;i<splitchar.length-1;i++){
System.out.print(" ");
for(int a=splitchar[i]+1;a<splitchar[i+1];a++)
System.out.print(arraychar[a]);
}
}
for(int i=splitchar[splitchar.length-1]+1;i<arraychar.length;i++){
System.out.print(arraychar[i]);
}
}
}