Title: String split and store in 2 different list
Tags: java;string;split
Question: I have a list which has data like this ```[1-123,2-456,6-654]```.
I need to split by delimiter "-" and store the first part in one List and second part in other list. 

```List clientIds = new ArrayList();
List chipIds = new ArrayList();
String delimiter = "-";
for(int i=0;i<selectedClientChips.size();i++){

// How to add them in 2 lists???

}
```

Comment: Is `[1-123,2-456,6-654]` a real element of your list or the structure of the complete list, which results in `1-123` as list objects (where the solution should be pretty easy)?

Comment: "*I need to split by delimiter "-"*" what is stopping you from doing so?


Here is another answer: To split the String 'N-NNNN' you can use the .split Method of the String class.

See the Java Doc:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

The split method returns a string array with the splitted values.

For the String ```"1-123"``` the ```.split("-");``` returns ```{"1", "123"};```

Improved Example:

```import java.util.*;
import java.lang.*;
import java.io.*;

public class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String selectedClientChips[] = {"1-123", "2-456", "6-654"};
        List clientIds = new ArrayList();
        List chipIds = new ArrayList();
        String delimiter = "-";
        for(int i=0;i<selectedClientChips.length;i++){
            String split[] = selectedClientChips[i].split(delimiter);
            if (split.length == 2) {
                clientIds.add(split[0]);
                chipIds.add(split[1]);
            }
        }
        System.out.println("CliendIDs: " + clientIds.toString());
        System.out.println("ChipIDs: " + chipIds.toString());
    }
}
```

Output:

```CliendIDs: [1, 2, 6]
ChipIDs: [123, 456, 654]
```

Working Example:

http://ideone.com/w2IvNm


Here is another answer: This should work...

```for(int i=0;i<selectedClientChips.size();i++){
    // get element and split it
    String element[] = selectedClientChips.get(i).split(delimiter);

    // add each part to one list
    clientIds.add(element[0]);
    chipIds.add(element[1]);
}
```


Here is another answer: This should work for your needs:

```    List<String&gt; selectedClientChips = new ArrayList();
    selectedClientChips.add("1-123");
    selectedClientChips.add("2-456");

    List clientIds = new ArrayList();
    List chipIds = new ArrayList();

    for(int i=0;i<selectedClientChips.size();i++){
        String[] r = selectedClientChips.get(i).split("-");
        clientIds.add(r[0]);

        chipIds .add(r[1]);
    }
```


Here is another answer: Add these three lines in the ```for``` loop:

``` String[] splitList = selectedClientChips.get(i).toString().split(delimiter);
 clientIds.add(splitList[0]);
 chipIds.add(splitList[1]);
```

Comment for this answer: @madhu Wait, why do you need to call `toString()` method? If type of elements in list is not `String` (which is confusing since your question is about "String split...") then you should mention it in your question and describe what exact type it is. Maybe this type contains methods like `getFirst()` or `getLast()` which returns parts before and after `-` and we don't even need to `split`.
Comment for this answer: @madhu Ohh.. I had considered the elements as String. Edited the answer as per your requirement.
Comment for this answer: Thanks its working but with small change:    clientIds.add(selectedClientChips.get(i).toString().split(delimiter)[0]);
     chipIds.add(selectedClientChips.get(i).toString().split(delimiter)[1]);

Here is another answer: ```Split``` and ```add``` first element in first list and next element in next list.

```String[] strArr;
for (int i = 0; i < selectedClientChips.size(); i++) {
    strArr = selectedClientChips.get(i).split(delimiter);
    clientIds.add(strArr[0]);
    chipIds.add(strArr[1]);
}
```


Here is another answer: You can try this :

```for(int i=0;i<selectedClientChips.size();i++){
String []splitArray=selectedClientChips.get(i).split(delimiter);
clientIds.add(splitArray[0]);
chipIds.add(splitArray[1]);
}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/08/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><string><split>', 'Title': 'String split and store in 2 different list', 'ViewCount': 118, 'id': 30703863, 'user_AboutMe': None, 'user_AccountId': 2010838.0, 'user_DisplayName': 'madhu', 'user_Reputation': 990.0, 'user_Views': 141.0, 'user_id': 1799300.0}}