VB.NET; 将逗号分隔的整数文件读入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20132135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:55:32  来源:igfitidea点击:

VB.NET; read file of comma-separated integers to an array

vb.netcsvfile-io

提问by Heiko Herlich

How can I read .txt file of comma-separated integers into array in VB.NET?

如何将逗号分隔的整数的 .txt 文件读入 VB.NET 中的数组?

回答by Mark Hall

You can try using Linq to Objects. I am assuming that your file has 1025 comma delineated integers in it.

您可以尝试使用Linq to Objects。我假设您的文件中有 1025 个逗号分隔的整数。

Dim myData As String = System.IO.File.ReadAllText("C:\Temp\Integers.txt") 'get your data
Dim myIntegerArray() As Integer = (From s As Integer In myData.Split(","c)).ToArray

回答by NoChance

Here is one way to do it. The correct code depends on how large is your file, how do you want to process the numbers, etc.

这是一种方法。正确的代码取决于您的文件有多大,您希望如何处理数字等。

Dim fileArray As String() = File.ReadAllLines("yourfilenamehere")

For i As Integer = 0 To fileArray.Length - 1
      'define array to hold each value in a cell of type string
       Dim arrayOfNumbersStoredAsStrings As String() = fileArray(i).Split(","C)
      'process the values in each line here...
Next