I am loading a txt file that I can split each row into an array (using "|" as the delimiter) using the split function.
However each row has a different number of elements within the array, so I cannot make a 2D array.
I'd like to somehow be able to combine a text string and an integer into a new variable.
Dim i As Integer = 0
Dim "row" & i As String = StringinRowi
To give
row0 = StringinRow0
And then loop this as many times as there are lines within the text file (I got this figured out)
Is this at all possible?
A 2D array might not work but a 2D List would work. Declare the list,
Dim data As New List(Of List(Of String))
Then cast the Split return .ToList.
data.Add(readlinestring.Split("|"c).ToList)
Now each row will be an index of data
and will contain a list of strings also indexed. Since everything is indexed, this should obviate the need for numbered variables.
Use a DataTable
. They're awesome.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dtbData As New DataTable
Dim strFilename As String = "C:\Junk\Junk.txt"
Using sr As New StreamReader(strFilename)
Do Until sr.EndOfStream
Dim strLine As String = sr.ReadLine()
Dim strFields() As String = Split(strLine, "|")
For i As Integer = dtbData.Columns.Count To strFields.GetUpperBound(0) 'add new columns if necessary
dtbData.Columns.Add("Column" + i.ToString)
Next i
dtbData.Rows.Add(strFields)
Loop
MsgBox("Row 2 Column 1 = " & dtbData.Rows(1).Item("Column1").ToString)
End Using
End Sub
End Class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.