In the previous post, I had difficulty accessing fields separated by comma in "~" separated rows.
Example "a,b,c~d,e,f~g,h,i~"

But after writing the output
I get the following result:

"a,b,c~

a,b,c~
d,e,f~

a,b,c~
d,e,f~
g,h,i~"

my desired result is as below:
"a,b,c~
d,e,f~
g,h,i~"

Any help is appreciated. Thanks!

 using (StreamWriter write = new StreamWriter(filePath, true))
 {
 string[] line = strInput.Split('~');
 foreach (string part in line)
  {

 string[] strField = part.Split(',');
 strDetails += strField[0] + "," + strField[5] + "," + strField[1] + "," + strField[2] + "," + strField[4] + "~";

 string[] strDetail = strDetails.Split('~');
 foreach (string parts in strDetail)
      {
         write.WriteLine(parts);
      }
 }

Any

Recommended Answers

All 10 Replies

If you don't need the data kept in memory after you save it you can write it directly from the first loop

     using (StreamWriter write = new StreamWriter(filePath, true))
    {
        string[] line = strInput.Split('~');
        foreach (string part in line)
        {
            string[] strField = part.Split(',');
            write.WriteLine(strField[0] + "," + strField[5] + "," + strField[1] + "," + strField[2] + "," + strField[4] + "~");
        }
    }

If you do need to keep it you can use a list:

        List<string> details = new List<string>();

            string[] line = strInput.Split(new char[]{'~'},StringSplitOptions.RemoveEmptyEntries);
            foreach (string part in line)
            {
                string[] strField = part.Split(',');
                details.Add(strField[0] + "," + strField[5] + "," + strField[1] + "," + strField[2] + "," + strField[4] + "~");
            }
            File.AppendAllLines(filePath,details);

oops forgot to comment on your code. You're writing to the file inside a nested loop. Which means that every time the outer loop iterates all the data is appended again

Hey thanks so much for the help.
I just added in some regex to the original code but the output that i get keeps repeating.

For example,
strInput= "orange~banana~banana~apple"
but what i get is this
output= "orange~orange banana~orange banana banana~orange banana banana apple"

I'm pretty sure my regex is right. Could you lend a helping hand? Would be much appreciated. Thanks!

 using (StreamWriter write = new StreamWriter(filePath, true))
 {

string[] line = strInput.Split('~');
foreach (string part in line)
{

 MatchCollection matches1 = Regex.Matches(part, @"\d{2}\\");
 string range = matches1[0].Value;
 switch (range)
 {
     case "00\\":
     strFruitType += "Apple";
     break;
     case "01\\":
     strFruitType += "Orange";
     break;
     case "02\\":
     strFuitType += "Banana";
     break;
}

write.WriteLine(strFruitType+"~");

                                            }

                                        }

hard to tell without a sample of strInput

I think i included in there alr..
strInput= "orange~banana~banana~apple"

Your regex can't be right it's looking for digits and you're sending it words.

Also your still wrtiting your data to the file on every iteration of the loop, instead after the loop is finished.

You really should learn to use Copy/Paste. You misspelled 'strFuitType' in one of the case blocks.

i have to admit i tweaked my original codes too much that i didn't realised my strInput's wrong.

And i want to print all the lines.
Maybe i should include my codes for better understanding.

strInput="2013063018,2,Name,001\Banana~"

 using (StreamWriter write = new StreamWriter(filePath, true))

  string[] line = strInput.Split('~');
  foreach (string part in line)
   {

   string[] strFields = part.Split(',');

   string date = strFields[0];
   DateTime dt;
   DateTime.TryParseExact(date, "yyyyMMddHH", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
    string strStartTime = dt.ToString("dd/mm/yyyy hh:00");
    string strEndTime = dt.ToString("dd/mm/yyyy hh:59");

   MatchCollection matches = Regex.Matches(strFields[3], @"\d{3}\\");
   string range = matches[0].Value;

   switch (range)
   {
    case "00\\":
    strFruitType += "Orange";
    break;
    case "01\\":
    strFruitType += "Banana";
    break;
   case "02\\":
    strFruitType += "Apple";
  break;

   }

  write.WriteLine(strStartTime + "," + strEndTime + "," + "," + strFruitType);     
     }

   }

The regex will extract '001\' but your case is looking for '01\'.

Also using += will add the fruit string to the previous one and the final line will contain all the fruit strings one after the other.

The format for strStartTime and strEndTime will return '00' for the month. Try 'MM' instead.

thanks ! already resolved this on my own :)

Please provide solution for update data available in csv file(in cpp)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.