我自己解决了......
int1 = CInt(line.Split(CChar(","))(1))
要逐行阅读文本文件的内容,我正在使用此代码
Using r As StreamReader = New StreamReader("0trace2.txt")
Dim line As String
line = r.ReadLine
Do While (Not line Is Nothing)
list.Add(line)
line = r.ReadLine
MsgBox(line)
Loop
End Using
但是,该文件有4个逗号分隔值,所有数字。我需要将每个数字提取为整数。我尝试了拆分方法,但我遇到了错误
Dim int1 as Integer
Dim int2 as Integer
Dim int3 as Integer
Dim int4 as Integer
Using r As StreamReader = New StreamReader("0trace2.txt")
Dim line As String
line = r.ReadLine
Do While (Not line Is Nothing)
list.Add(line)
line = r.ReadLine
'MsgBox(line)
int1 = CInt(line.Split(CChar(","))(1))
Loop
End Using
由于
答案 0 :(得分:1)
假设您只需要从具有
结构的文件中收集整数1,2,3,4
5,6,7,8
9,10,11,12
你可以使用List(Of Integer)全部收集它们,例如:
Dim arr As New List(Of Integer)
Do While (Not line Is Nothing)
For Each s In line.split(",")
arr.Add(s)
Next
line = r.ReadLine
Loop
答案 1 :(得分:1)
也许更容易的方法是:
Dim IntList = Line.Split(","c).ToList.ConvertAll(Function(x) CInt(x))
或使用linq
Dim IntList = (From x In Line.Split(","c) Select CInt(x)).ToList
注意:请不要就CInt()与Integer.Parse()vs Int32.Parse()
进行辩论