我有一个文本文件,如:
[编辑]行数未知,可能是数百行。
我如何将它们存储在多维数组中?我希望我的数组看起来像:
sample(0)(0) = "--------"
sample(0)(1) = "Line1"
..and so on
sample(1)(0) = "--------"
sample(1)(3) = "Sample 123"
..and so on
到目前为止我所做的是打开文件并以1维数组存储:
logs = File.ReadAllLines("D:\LOGS.TXT")
我尝试过创建一个字符串数组,如:
Dim stringArray as String()()
stringArray = New String(varNumber0)(varNumber1)
但它会返回并出错。
答案 0 :(得分:0)
尝试这样但你需要根据你自己定制
Dim mArray(10,10) As String
Dim i As Integer = 0
For I=0 to 10
For J=0 to 10
For Each line As String In System.IO.File.ReadAllLines("file.txt")
mArray(i,j) = cmdReader.Item(line)
Next
Next
Next
答案 1 :(得分:0)
使用这样的声明(这只是一个通用的)
Dim dim1 As Integer = 0
Dim dim2 As Integer = 0
Dim strings(,) As String
Do
dim1 = NewDimensionNumberFromFile
dim2 = NewSecondDimensionNumberFromFile
ReDim Preserve strings(dim1, dim2)
strings(dim1, dim2) = ValueFromfile
Loop While (Not EOF()) 'this will determine
答案 2 :(得分:0)
您可以使用File.ReadLines
/ File.ReadAllLines
来获取行,并使用简单的For Each
- 循环来填充List(Of List(Of String))
。然后你可以使用
list.Select(Function(l) l.ToArray()).ToArray()
获取String()()
(锯齿状数组):
Dim lines = File.ReadLines("D:\LOGS.TXT")
Dim list As New List(Of List(Of String))
Dim current As List(Of String)
For Each line As String In lines.SkipWhile(Function(l) Not l.TrimStart.StartsWith("----------"))
If line.TrimStart.StartsWith("----------") Then
current = New List(Of String)
list.Add(current)
Else
current.Add(line)
End If
Next
Dim last = list.LastOrDefault()
If last IsNot Nothing Then
If Not current Is last AndAlso current.Any() Then
list.Add(current)
ElseIf Not last.Any() Then
list.Remove(last) ' last line was ("----------")'
End If
End If
Dim stringArray As String()() = list.Select(Function(l) l.ToArray()).ToArray()
如果要在第一个位置包含数组中的---------
:
For Each line As String In lines.SkipWhile(Function(l) Not l.TrimStart.StartsWith("----------"))
If line.TrimStart.StartsWith("----------") Then
current = New List(Of String)
current.Add(line)
list.Add(current)
Else
current.Add(line)
End If
Next