I tried this code to convert a string into sub-strings until the original string do not empty. Please guide where I'm wrong in it? Thanks.

    String dates = '01/13/2015,01/20/2015,01/27/2015,02/03/2015,02/10/2015,02/17/2015,

    Integer index=0;
    while(dates !== ""){

        //Or if possible to create an array of Date object.
        String[] eachDt = new String[]{}; 
        eachDt[index] = dates.substringBefore(",");


2 Answers 2


I don't find the substringBefore method in String methods. Please correct me if I am wrong.

In my opinion, you are simply doing:

List<String> eachDt = dates.split(',');

Just use that.

  • Here it is goo.gl/qgLs0T And this line of code will separate each date from date string?
    – Superman
    Jan 16, 2015 at 4:21

you can avoid loop .. just do this :

String[] eachDt = new String[]{}; eachDt = dates.substringBefore(",");

and then use loop if you want to iterate the list. You don't need index variable just to populate the list/array. substring() method returns list anyways.

  • My bad .. i was talking about split too.. it should be dates.split(","); instead of substring
    – Manju
    Jan 16, 2015 at 5:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .