user3430861
user3430861

Reputation: 182

Reading from a string List<>

my application basically reads a CSV file which will always have the same format and I need to application to create a CSV file with different formatting. Reading and writing CSV file is not the issue, however the problem I am having is reading from the string array containing all the data from the CSV file.

For example: From the below, how can I get the system to get me the 4th value only: Value Date

[0] = "\"Book Date\",\"Reference\",\"Descript\",\"Value Date\",\"Debit\",\"Credit\",\"Closing Balance\""

This is how I am reading from CSV file.

        openFileDialog1.ShowDialog();
        var reader = new StreamReader(File.OpenRead(openFileDialog1.FileName));
        List<string> searchList = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            searchList.Add(line);
        }

Upvotes: 0

Views: 199

Answers (2)

Gergely Bakos
Gergely Bakos

Reputation: 90

split the line and get the 4th value like this:

searchList.Add(line.Split(',')[3]);

Upvotes: 1

user1672994
user1672994

Reputation: 10849

Use String.Split. It returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

var splitStrings = line.Split(",");

if (splitStrings.Length > 4)
{
   searchList.Add(splitStrings[3]);
}

Upvotes: 3

Related Questions