I have a list that I generated with a text file. I'm going through the list and using StringBuilder so I can use the StringBuilder object to create a csv file. The file has a ^ when its starting on a new record. I need to be able to append a new line to the stringbuilder object when the List returns a string that has the ^ in it, but as you can see with my code below, I can never tell when to append the new line and I don't know how to fix it.
Here is one of the ways I tried and the issue shows in the comments
foreach (var a in lstData)
{
if (a.Contains("^"))
{
string strName = Regex.Replace(a, "<.*?>", string.Empty).Remove(0, 1);
sbQ.Append(strName + ",");
}
else
{
sbQ.Append(a + ",");
}
if (a.Contains("^")) sbQ.AppendLine();
// Doesn't work, always appendlines when it encounters '^'
// should only append new line on next occurence
}
and here is the other way.
string strZ;
string strX;
foreach (var a in lstData)
{
if (a.Contains("^"))
{
strZ = Regex.Replace(a, "<.*?>", string.Empty).Remove(0, 1);
sbQ.Append(strZ + ",");
strX = strZ;
}
if (strZ == strX)
{
// Causes Error : Use of Unassigned local variable 'strX'
}
}
I've tried using a do/while statement by iterating through a copy of the List and didn't work either.
EDIT
The data in the text file looks like this..
^<a class= ........>Name</a>
value1
value2
value3
value4
etc....
^<a class=......>Name</a>
value1
value2
value3
value4
etc....
^<a class=......>Name</a>
etc...
What I am trying to accomplish
name, value1, value2, value3, value4, etc...
name, value1, value2, value3, value4, etc...
EDIT Results in csv.

Where you see the link, that had a ^ before it and when that gets reached then it should move on to line 2, then when it hits the ^ again to line 3 and so forth. Its a new record when it hits the ^
This is how it should look...

EDIT/UPDATE
Physician Name, (CPSO#)
Primary Practice Location
Disciplinary Info & Restrictions
^Aal Ali, Saleh Saif Salem A S Fares (#82358)
P O Box: 8313
Abu Dhabi 0000
United Arab Emirates
Phone: +971506117644
^Aalders, Ryan Francis (#103559)
Kingston General Hospital
Department of Family Medicine
76 Stuart Street
Kingston ON K7L 2V7
Phone: (613) 533-9300
^Aarabi, Mehdi (#81281)
UHN Toronto Western Hospital
2nd Floor East Wing
399 Bathurst Street
Toronto ON M5T 2S8
Phone: (416) 603-5641
^Aaron, Shawn David (#62311)
Ottawa General Hospital
501 Smyth Road
Ottawa ON K1H 8L6
Phone: (613) 737-8899 Ext. 74729
Fax: (613) 739-6807
lstData? Is it the string that containsa,b^c,d? You want to split on^and then,? Are there any considerations for escape characters? Is what you want justlstData.Split('^').Select(x => x.Split(',')).ToArray()? i.e. return an array of string arrays where each array contains an array of the comma separated fields?x.Split(^)? Sorry, I'd post that as an answer, but I'm still not 100% of your requirements.