Reputation: 619
Assume i want to split,
String line = "ABCDEFG";
Into,
{"ABCD","EFG"}
I would do this,
String[] alpha = line.split('D');
But is gives me,
{"ABC","EFG"}
Notice that the 'D' is missing, how do i split it while keeping the character?
Upvotes: 3
Views: 81
Reputation: 110
I did a function to do what you want.
public static String[] splitWise(String line,char a){
String[] res= {"",""};
Boolean added=false;
for(int i=0;i<line.length()-1;i++)
{
if(line.charAt(i)==a)
{
//change next line if you want the element you split in the second element of the array
res[0]+= line.charAt(i);
added=true;
}else if(!added){
res[0] += line.charAt(i);
}else
res[1] += line.charAt(i);
}
return res;
}
I think it's what you want. You just need to call the function like this:
String[] alpha = splitWise(line,'D');
Upvotes: 1