-1

I'm pretty new in Java and now i have following problem which I'm trying to solve.

I have a string like this:

item1item2,item3,item4item5,item6,item7

and what i need to get is this 2 lists:

{item2,item3,item4}
{item5,item6,item7}

Please note that item1 is being omitted in this list and also there's no comma between item4 and item5

Thanks in advance

errordmas
  • 65
  • 1
  • 8
  • You are going to want to look at `String.split()`. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String) – jlars62 Jun 13 '14 at 23:44
  • Once you split things between the commas, how do you know which parts of which pieces to drop & which to split? – Scott Hunter Jun 13 '14 at 23:46
  • That's what i did in the first place but my case is kind of specific, as it's not just simple String.split() function. – errordmas Jun 13 '14 at 23:46
  • Why don't you do two different things? Split, then take care of your first element? – awksp Jun 13 '14 at 23:47
  • Scott, sorry but i didn't understand your question.. can you please rephrase it? – errordmas Jun 13 '14 at 23:48

2 Answers2

1

It would probably be easiest to first split this into two strings using the subString() method, the first containing items 2, 3, 4 and the second 5, 6, 7. Then after that you can use String.split() to get what you want.

jhobbie
  • 1,016
  • 9
  • 18
  • I just tried using substring to remove first several characters that i don't need and it worked on the first part when i need to split between item1 and item2 to start with item2, i guess the same thing i can do with the rest of the string. Thanks – errordmas Jun 14 '14 at 00:01
1

This will work in this specific case.

String string = "item1item2,item3,item4item5,item6,item7";
string = string.substring(5, string.length);
String[] list1 = string.substring(0, string.length/2).split(',');
String[] list2 = string.substring(string.length/2, string.length).split(',');
Jasper-M
  • 14,966
  • 2
  • 26
  • 37