仅从文本文件中读取整数并分离
Reading integers from a text file only and separating
我创建了一个程序,它只成功地从包含字符串的文本文件中读取整数,并将其读取到列表框中。例如:
文本文件包含:
- 泰勒 7
- 丹尼尔 2
- 迈克 6
- 罗里 9
然后列表框将显示:7269。
但是我计划将这些数字从最高到最低排序(反之亦然),为了做到这一点,我想尝试将每个数字存储在列表框中的新行中,但我不确定如何完成此操作。
我用来读取整数的代码是:
Dim intOnly As String = IO.File.ReadAllText(File_1)
Dim intValue As String = String.Empty
For Each c As Char In intOnly
If IsNumeric(c) Then
intValue += c
End If
Next
ListBox3.Items.Add(intValue)
End Sub
利用 ListBox.DataSource
属性 和数组,您可以在新行中显示每个数字。
例如:
aLstSrc = New Integer(2) {}
aLstSrc(0) = 1
aLstSrc(1) = 2
aLstSrc(2) = 3
ListBox1.DataSource = aLstSrc
在您的示例中,您应该启动数组并在 For Each
循环中填充。
您可以将数组创建为字符串或整数,但如果您需要对数据进行进一步处理,整数应该是最佳选择。
另请参阅此处以获取有关将文本文件处理为数组/列表的更多信息:VB.NET - Reading a text file containing tab-separated integers/doubles and storing them in arrays
尝试这样的事情:
Dim values As New List(Of Integer)
Dim File_1 As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "SomeFile.Txt")
For Each line As String In System.IO.File.ReadLines(File_1)
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "\d+")
If m.Success Then
values.Add(Convert.ToInt32(m.Value))
End If
Next
values.Sort()
values.Reverse()
ListBox3.DataSource = values
试试这个:
Private Sub test()
Dim sr As New System.IO.StreamReader("file.txt")
Dim LofInt As New List(Of String)
While sr.Peek <> -1
Dim line As String = sr.ReadLine
Dim intValue As String = String.Empty
For Each c As Char In line
If IsNumeric(c) Then
intValue += c
End If
Next
LofInt.Add(intValue)
End While
sr.Close()
LofInt.Sort()
For Each i In LofInt
ListBox1.Items.Add(i)
Next
End Sub
基本上我们所做的是:
我们逐行读取您的文件并存储数值。在每一行之后,我们将我们所拥有的添加到列表中。
之后我们使用该列表的函数对 "numers"(仍然是一个字符串,所以我们应该说文本)进行排序并将它们添加到您的列表框
我创建了一个程序,它只成功地从包含字符串的文本文件中读取整数,并将其读取到列表框中。例如: 文本文件包含:
- 泰勒 7
- 丹尼尔 2
- 迈克 6
- 罗里 9
然后列表框将显示:7269。 但是我计划将这些数字从最高到最低排序(反之亦然),为了做到这一点,我想尝试将每个数字存储在列表框中的新行中,但我不确定如何完成此操作。 我用来读取整数的代码是:
Dim intOnly As String = IO.File.ReadAllText(File_1)
Dim intValue As String = String.Empty
For Each c As Char In intOnly
If IsNumeric(c) Then
intValue += c
End If
Next
ListBox3.Items.Add(intValue)
End Sub
利用 ListBox.DataSource
属性 和数组,您可以在新行中显示每个数字。
例如:
aLstSrc = New Integer(2) {}
aLstSrc(0) = 1
aLstSrc(1) = 2
aLstSrc(2) = 3
ListBox1.DataSource = aLstSrc
在您的示例中,您应该启动数组并在 For Each
循环中填充。
您可以将数组创建为字符串或整数,但如果您需要对数据进行进一步处理,整数应该是最佳选择。
另请参阅此处以获取有关将文本文件处理为数组/列表的更多信息:VB.NET - Reading a text file containing tab-separated integers/doubles and storing them in arrays
尝试这样的事情:
Dim values As New List(Of Integer)
Dim File_1 As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "SomeFile.Txt")
For Each line As String In System.IO.File.ReadLines(File_1)
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "\d+")
If m.Success Then
values.Add(Convert.ToInt32(m.Value))
End If
Next
values.Sort()
values.Reverse()
ListBox3.DataSource = values
试试这个:
Private Sub test()
Dim sr As New System.IO.StreamReader("file.txt")
Dim LofInt As New List(Of String)
While sr.Peek <> -1
Dim line As String = sr.ReadLine
Dim intValue As String = String.Empty
For Each c As Char In line
If IsNumeric(c) Then
intValue += c
End If
Next
LofInt.Add(intValue)
End While
sr.Close()
LofInt.Sort()
For Each i In LofInt
ListBox1.Items.Add(i)
Next
End Sub
基本上我们所做的是: 我们逐行读取您的文件并存储数值。在每一行之后,我们将我们所拥有的添加到列表中。 之后我们使用该列表的函数对 "numers"(仍然是一个字符串,所以我们应该说文本)进行排序并将它们添加到您的列表框