我正在创建一个程序来创建一个格式如下的文本文件:
[1,* John,Doe,Family,0002354561]
[2,* Jason,Doe,Obstetric,0002358411]
[3,* Mikael,Doe,Pediatric,0002352361]
[4,* Jamiel,Doe,Orthopedic,0002354547]
我需要的是一种读取文件并获取最后一行的第一个数字的方法。然后把它放到一个整数。
我知道我可以这样读第一行:
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader(("..\..\..\Patients.txt"))
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)
但是我如何读取最后一行并将第二个字符串转换为整数变量?
答案 0 :(得分:0)
我正在使用类似的东西
Dim lastLine As String = File.ReadLines(myFileName) _
.Where(Function(f As String) (Not String.IsNullOrEmpty(f))).Last.ToString
Dim startpos As Integer = lastLine.IndexOf(",", 3)
lastline = Substring(lastline, startpos).Trim
Dim firstchar as String = Substring(lastline,0,1)
Dim result As Integer
If Integer.TryParse(firstchar, result) Then Return result _
Else Return -1
答案 1 :(得分:0)
以下是如何完成任务的示例,这会将所有数字放入arraylist中。不是通过将其转换为长(或整数),它将删除任何前导零
Public Function GetDigits() As System.Collections.ArrayList
Dim numList As System.Collections.ArrayList
numList = New System.Collections.ArrayList()
Dim fileReader As System.IO.StreamReader
fileReader = My.Computer.FileSystem.OpenTextFileReader(("C:\Patients.txt"))
Dim stringReader As String
While Not fileReader.EndOfStream
stringReader = fileReader.ReadLine()
If Trim(stringReader) = "" Then Continue While
Dim strParts
strParts = Split(stringReader, Chr(44))
Try
numList.Add(Integer.Parse(Trim(Replace(strParts(UBound(strParts)), "]", ""))))
Catch
MsgBox("Could not parse string: " & stringReader)
End Try
End While
fileReader.Close()
GetDigits = numList
End Function