Click here to Skip to main content
15,920,030 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a .txt file which looks like this:
VB
09:00-16:00
19:00-23:00
10:00-17:00
20:00-24:00
07:00-14:00


I simply want to load each line in a List<string>, split each line in "-", and save it in another List<string>.

my code:
string line;
            while ( (line=reader.ReadLine()) != null)
            {
                // this is a first list
                schedule_initially.Add(line);
            }
           
            
            // Thsi is the second list
            List<string> final_schedule = new List<string>();
            ArrayList sch= new ArrayList();
            foreach (string s in schedule_initially)
            {
                if(s !=null)
                sch.Add(s.Split('-'));
            }

            foreach (string v in sch)
            {
                final_schedule.Add(v);
            }
             
            // Show the result to a label
            foreach (string s in final_schedule)
            {
                label1.Text += "\n" + s;
            }



But compiler shows error.
Any ideas ?
Posted
Comments
Tomas Takac 26-Jan-15 9:16am    
What error? Compiler or runtime? Change sch from ArrayList to List<string[]> then you will see what's the problem.
Zoltán Zörgő 26-Jan-15 9:22am    
Quite vague. If you have 5 elements, by spitting them you will have 10. Supposing you have the rows you have posted in the file, what do you want to have at the end?

I would recommend you to use LINQ:
C#
final_schedule = schedule_initially.SelectMany(x => x.Split('-')).ToList();
 
Share this answer
 
s.Split('-') 

returns a System.String[] type, as such,
sch.Add(s.Split('-')) 

adds the String[] to sch, not the intended values, change it to:
C#
string[] temp = s.Split('-'));
for each (string s in temp)
{
    sch.Add(s);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900