Reputation: 11
I am given this .txt file.
Left Behind,Lahaye,F,7,11.25
A Tale of Two Cities,Dickens,F,100,8.24
Hang a Thousand Trees with Ribbons,Rinaldi,F,30,16.79
That file is: Book Title, Author ,Fiction or Nonfiction, Stock ,Price
I need to split those into multiple arrays, at least I feel like I do, I have this so far
Private Sub frmInventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate array
Dim temp() As String = IO.File.ReadAllLines("Books.txt")
temp = Split(",")
lstBooks.DataSource = temp.ToList
End Sub
which isn't doing the trick... obviously.
I just don't know how to make it put it into multiple arrays, such as book, etc. or maybe a 2 dimensional array.
Thanks for any help.
I have looked on many sites like this, but the only help they offer is splitting it in 2. http://patorjk.com/programming/tutorials/vbarrays.htm#splitfunction
Upvotes: 1
Views: 4658
Reputation: 460108
If the format is always so strict, you can do it manually with string.Split, otherwise i would suggest to use an existing library like FileHelpers
, this fast CSV parser or the VB.NET onboard TextFieldParser
class.
To answer your actual question, you can use an File.ReadLines
and an IEnumerable(Of String())
.
Dim lines As IEnumerable(Of String()) =
From line In IO.File.ReadLines("Books.txt")
Select line.Split(","c)
If you want an array: lines.ToArray()
, that will load all into memory(like File.ReadAllLines
) whereas File.ReadLines
streams the lines from the file and only if you ask for(f.e. via Take(10)
).
Edit: If you want the most reusable approach, use a custom class with these properties and initialize it from the string():
Public Class Book
Public Property Title As String
Public Property Author As String
' and so on '
End Class
Dim books = From line In System.IO.File.ReadLines("Books.txt")
Let parts = line.Split(","c)
Select New Book() With {
.Title = parts(0),
.Author = parts(1)
}
You can use this for example in a For Each
or as DataSource. Note that it is error-prone, the title probably contains also commas or the format is not always strict.
Upvotes: 2
Reputation: 20469
Without knowing what you want to do with the data, its hard to help. But i imagine your best bet would be to create a book class, with properties for Book Title, Author ,Price etc.
Create a list(of book), read the txt file line by line, splitting each line on commas, and creating a book instance with each resulting array, and add it to the list, like this hastily written example:
Public Class book
Property title As String
Property author As String
Property price As Double
Public Sub New(ByVal title As String, ByVal author As String, ByVal price As Double)
_title = title
_author = author
_price = price
End Sub
End Class
Public Class Form1
Public booklist As List(Of book)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bookfile As String() = IO.File.ReadAllLines("c:\file.txt")
For Each line As String In bookfile
Dim bookinfo As String() = line.Split(",")
Dim abook As New book(bookinfo(0), bookinfo(1), bookinfo(2))
booklist.Add(abook)
Next
End Sub
End Class
If this is the kind of thing you would like to do, i will improve the above code as needed
Upvotes: 0