Dealing with commas in a CSV file

后端 未结 27 2798
傲寒
傲寒 2020-11-21 06:53

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name.

27条回答
  •  长发绾君心
    2020-11-21 07:20

    I usually do this in my CSV files parsing routines. Assume that 'line' variable is one line within a CSV file and all of the columns' values are enclosed in double quotes. After the below two lines execute, you will get CSV columns in the 'values' collection.

    // The below two lines will split the columns as well as trim the DBOULE QUOTES around values but NOT within them
        string trimmedLine = line.Trim(new char[] { '\"' });
        List values = trimmedLine.Split(new string[] { "\",\"" }, StringSplitOptions.None).ToList();
    

提交回复
热议问题