-2

I am importing a csv file and a column in this csv file can have commas in it. So when I import it increases the columns .

3,Stage3,"Desc Stage,test,test2",55.98,98.76

This is the row and I want my columns to be :

3,
Stage3,
"Desc Stage,test,test2",
55.98,
98.76
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92

1 Answers1

0

Try This

    public static string[] splitCSV(string CSVLineWithQuotedTextWithCommas)
    {
        Regex regExToSeperate = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
        List<string> result = new List<string>();
        string curr = null;
        foreach (Match match in regExToSeperate.Matches(CSVLineWithQuotedTextWithCommas))
        {
            curr = match.Value;
            if (0 == curr.Length)
            {
                result.Add("");
            }

            result.Add(curr.TrimStart(','));
        }
        return result.ToArray<string>();
    }
Srinika Pinnaduwage
  • 1,044
  • 1
  • 7
  • 14