Store CSV file values into array
Suppose you have successfully read a CSV file containing two columns (semicolon separated) using StreamReader
and Split()
. To store each column into a different array, follow these steps:
listA
and listB
. while
loop to iterate over the CSV file line by line. Split()
function, which will decompose the values based on the semicolon separator. listA
and the second value (column 2) to listB
. Here is a sample code that demonstrates this process:
<code class="language-csharp">using System.IO; static void Main(string[] args) { using (var reader = new StreamReader(@"C:\test.csv")) { List<string> listA = new List<string>(); List<string> listB = new List<string>(); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); listA.Add(values[0]); listB.Add(values[1]); } } }</code>
With this approach you can effectively store each column of your CSV file into a separate array for further processing or display.
The above is the detailed content of How Can I Store CSV File Columns into Separate Arrays in C#?. For more information, please follow other related articles on the PHP Chinese website!