Question

I need to read a .csv file into an array but I don't want the first row of the .csv file to be in the array. How do I exclude it?

'Create array.
        Dim sReader As New StringReader(strBuffer)
        Dim List As New List(Of String)
    Do While sReader.Peek >= 0
        List.Add(sReader.ReadLine)
    Loop
        Dim lines As String() = List.ToArray
        sReader.Close()
Was it helpful?

Solution

Could you just remove the first element in the list?

List.RemoveAt(0);

OTHER TIPS

You could also do a readline before the loop:

'Create array.
        Dim sReader As New StringReader(strBuffer)
        Dim List As New List(Of String)
        sReader.ReadLine
    Do While sReader.Peek >= 0
        List.Add(sReader.ReadLine)
    Loop
        Dim lines As String() = List.ToArray
        sReader.Close()

You could simplify your code alot by using

Dim lines As String() = File.ReadAllines("MycsvFile.csv")
lines.RemoveAt(0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top