我正在考虑在 VB.NET 中创建一个应用程序,并且在对新行上的文件进行排序时遇到了一些问题。
基本上在新行上,我想捕获该行中的数据,并将其存储在一个数组中以备后用。
谁能帮我这个?将不胜感激:)
PHP 中的示例:
$data = explode("\n", $a);
好的,System.IO.File.ReadAllLines()
返回 astring
Array
所以你可以保存返回值。
或者,您可以使用for
orforeach
循环迭代结果,或使用linq
to 对其进行一些处理。选项很多,取决于你想做什么。
如果我正确理解您的问题,请使用它来读取文件并逐行加载到数组中。
Dim myArray() = New String() {} 'Corrected array, instantiated. or use myArray(-1)
Dim x as Integer = 0
Using reader As StreamReader = New StreamReader("file.txt")
holdData(x) = reader.ReadLine 'Reads line by Line and stores in array.
x += 1 'Increase array index by 1 before moving to next line
End Using
编辑:另一种方法,我不知道,我觉得很有趣的是:
Dim path As String = "data.txt"
Dim holdData() As String = IO.File.ReadAllLines(path)
这是两行代码,与我的原始响应相同。非常高效,您需要做的就是使用holdData()
数组将文本行读取到您希望使用它的 、textbox
或w/e :)label
listbox