• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

C# String.split method on a .csv file

I've got the attached .csv file.  Each record is separated by returns, and the fields in each record are separated by commas.  I've already got the entire contents of the file read into my application.  I need to use the string.split method on it to separate the records into different array elements.  So I need to split the file up by the return character that exists between records.  I've tried using .split('\n'), but that doesn't seem to be working.  Any ideas how I can express that character in my code?

I have the file uploaded as a .txt file, but it is actually a .csv by default.
clearcheckbook.txt
0
josephdaviskcrm
Asked:
josephdaviskcrm
  • 2
1 Solution
 
numberkruncherCommented:
The following works for me:

            string filePath = @"C:\Downloads\clearcheckbook.csv";
 
            try
            {
                StreamReader reader = new StreamReader(filePath);
                string csvData = reader.ReadToEnd();
 
                string[] rows = csvData.Split('\n');
 
                string[][] data = new string[rows.Length][];
                int ri = 0;
                foreach (string row in rows)
                    data[ri++] = row.Split(',');
 
                // Get row 3 and column 2 from data.
                string[] row3 = data[2];
                string row3_col2 = row3[1];
            }
            catch (Exception)
            {
                // An error has occurred!
            }

Open in new window

0
 
numberkruncherCommented:
First I read the entire file into a string (much as you probably do). Then the string is split using the newline character, and split again for each row using the comma character.

In the above example each row is represented as an array of strings. Data is simply an array of rows. You might want to create a better data structure around this:
// Updated function (see class below)
private void button1_Click(object sender, EventArgs e)
{
    string filePath = @"C:\Downloads\clearcheckbook.csv";
 
    try
    {
        StreamReader reader = new StreamReader(filePath);
        string csvData = reader.ReadToEnd();
 
        string[] rows = csvData.Split('\n');
 
        // Extract data.
        Record[] data = new Record[rows.Length];
        int ri = 0;
        foreach (string row in rows)
            data[ri++] = new Record(row.Split(','));
 
        // Get row 3 and column 2 from data.
        Record record3 = data[2];
        string account = record3.Account;
    }
    catch (Exception)
    {
        // An error has occurred!
    }
}
 
// Class:
public class Record
{
    public Record(string[] cols)
    {
        Columns = cols;
    }
 
    public string[] Columns { get; private set; }
 
    public string Date
    {
        get { return Columns[0]; }
        set { Columns[0] = value; }
    }
    public string Amount
    {
        get { return Columns[1]; }
        set { Columns[1] = value; }
    }
    public string Description
    {
        get { return Columns[2]; }
        set { Columns[2] = value; }
    }
    public string Category
    {
        get { return Columns[3]; }
        set { Columns[3] = value; }
    }
    public string Account
    {
        get { return Columns[4]; }
        set { Columns[4] = value; }
    }
    public string Jived
    {
        get { return Columns[5]; }
        set { Columns[5] = value; }
    }
}

Open in new window

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now