Convert a complicated txt file into a csv file using c# -


i have absolutely no idea here snippet of file i'm trying convert:

"september  3beef lamb chops  4fish not fish  5mac , cheese pb & j" 

the csv file supposed print date comes after in quotes, above should like:

tuesday september third 2013 "beef" "lamb chops"

wednesday september fourth 2013 "fish" "not fish"

thursday september fifth 2013 "mac , cheese" "pb&j"

here have far:

streamreader reader = new streamreader(@"..\..\lunches.txt");  while (!reader.endofstream) {     string currentline = reader.readline(); }  streamwriter writer = new streamwriter(@"..\..\lunches.csv");  // date.tostring("ddddd yyyyy mm mmmmmm"); string delimiter = ","; 

this output

here code

 private void form1_load(object sender, eventargs e)     {         string sayka = "august\n\n" +             "31beef\n" +             "lamb chops\n" +              "24fish\n" +             "not fish\n" +              "15mac , cheese\n" +             "pb & j\n";         messagebox.show(makecsv(sayka));     }      string getmonthname(int val)     {         switch (val)         {             case 1: return "january";             case 2: return "february";             case 3: return "march";             case 4: return "april";             case 5: return "may";             case 6: return "june";             case 7: return "july";             case 8: return "august";             case 9: return "september";             case 10: return "october";             case 11: return "november";             case 12: return "december";             default: return null;         }     }      string getdayname(int val)     {         switch (val)         {             case 1: return "first";             case 2: return "second";             case 3: return "third";             case 4: return "fourth";             case 5: return "fifth";             case 6: return "sixth";             case 7: return "seventh";             case 8: return "eighth";             case 9: return "nineth";             case 10: return "tenth";             case 11: return "eleventh";             case 12: return "twelth";             case 13: return "thirteenth";             case 14: return "fouteenth";             case 15: return "fifteenth";             case 16: return "sixteenth";             case 17: return "seventeenth";             case 18: return "eighteenth";             case 19: return "nineteenth";             case 20: return "twentieth";              default: return "";         }     }      string getdayname2(int val)     {         if (val == 30) return "thirtieth";         else if (val > 30) return "thirty " + getdayname(val % 30);         else if (val > 20) return "twenty " + getdayname(val % 20);         else return getdayname(val);     }      string makecsv(string val)     {         string res = "";         string[] ss = val.split('\n');         int curmonth = 0;         (int = 0; < ss.length; i++)         {             if (ss[i].trim() != "")             {                 bool isint = false;                 try                 {                     int inta = convert.toint32(ss[i][0].tostring());                     isint = true;                 }                 catch { }                  if (isint)                 {                     bool isdoubleint = false;                     try                     {                         int intb = convert.toint32(ss[i][1].tostring());                         isdoubleint = true;                     }                     catch { }                      int date = 0;                     if (isdoubleint) date = convert.toint32(ss[i].remove(2));                     else date = convert.toint32(ss[i][0].tostring());                      datetime dt = new datetime(datetime.now.year, curmonth, date);                      string itemname = "";                     if (isdoubleint) itemname = ss[i].substring(2);                     else itemname = ss[i].substring(1);                      string itemname2 = ss[i + 1];                     res += dt.dayofweek + " " + getmonthname(dt.month) + " " + getdayname2(dt.day) + " \"" + itemname + "\"" + " \"" + itemname2 + "\"\n";                 }                 else                 {                     (int j = 1; j < 13; j++)                         if (ss[i].toupper().startswith(getmonthname(j)))                         {                             curmonth = j;                             break;                         }                 }             }         }         return res;     } } 

from filestream either use streamreader.readtoend(), string , use function, or if file big use line line..

rate if helps..


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

mysql - Pass value between different pages (form) with PHP -