Reputation: 182
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
Reputation: 90
split the line and get the 4th value like this:
searchList.Add(line.Split(',')[3]);
Upvotes: 1
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