When a separator character occurs inside a quoted string, the number of the double quotation characters is always an odd number. So step through a line one character at a time keeping a record of whether or not the double quote count is even or odd. If a separator character is found when the quoted count is odd, the current substring is a quoted substring. So wait until a separator is found when the quoted count is even before extracting that substring. I wrote the following many years ago as an exercise and you are welcome adapt it for your own use. But, as has already been suggested, you are better off using an existing library version than writing your own code.
public class Reader
{
public List<List<string>> ReadCsv(string[] data, char separator)
{
var rows = new List<List<string>>();
foreach (string text in data)
{
bool isquotedString = false;
int startIndex = 0;
int endIndex = 0;
var cols = new List<string>();
foreach (char c in text)
{
if (c == '"')
{
isquotedString = !isquotedString;
}
if (c == separator && !isquotedString)
{
cols.Add(trimQuotes(text.Substring(startIndex, endIndex - startIndex)));
startIndex = endIndex + 1;
}
endIndex++;
}
cols.Add(trimQuotes(text.Substring(startIndex, endIndex - startIndex)));
rows.Add(cols);
}
return rows;
}
private string trimQuotes(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
return text[0] == '"' && text.Length > 2
? text.Substring(1, text.Length - 2).Replace("\"\"", "\"")
: text.Replace("\"\"", "\"");
}
}