1

我自己解决了...

  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

谢谢

4

2 回答 2

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
于 2013-10-07T01:43:13.300 回答
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() vs Integer.Parse() vs Int32.Parse()

于 2014-02-19T22:15:02.487 回答