0

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,
02/24/2015,03/03/2015,03/10/2015,03/17/2015,03/24/2015,03/31/2015,04/07/2015,
04/14/2015,04/21/2015,06/10/2015';

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

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

    System.debug(eachDt);

2 Answers 2

0

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.

1
  • 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
0

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.

1
  • 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 .