特定分隔符后读取行

时间:2013-11-15 22:37:27

标签: vb.net

我的问题是我不明白如何在lblBoxes中输出来自txt.file的正确数据 我的形式有8个lbloutputs,我正在尝试修剪一切,包括:。 这不起作用,我理解它是在第一行找到索引:但在第二行使用它的子字符串。我还想创建数组以首先找到所有索引,然后将它们与子字符串一起使用,但它不会让我在列表中以我这样做的方式。数组示例:

    'EXAMPLE OF BAD ARRAY
    Dim intIndexDelimiter() As Integer

    For Each i As Integer In employeeDataFileInput.ReadToEnd.IndexOf(":")
        intIndexDelimiter = i
    Next

代码:

    Public Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strFileName As String                        'hold name fo the file
        Dim employeeDataFileInput As StreamReader        'object variable

        'get name for file
         strFileName = InputBox("Enter Name Of the File", "Open File")

        If File.Exists(strFileName) Then
        'open file 
        employeeDataFileInput = File.OpenText(strFileName)
        MessageBox.Show(strFileName & "File open")
    Else
        MessageBox.Show(strFileName & "This file does not exist.")
    End If

    'not working 
    lblRecordNum.Text = CStr(employeeDataFileInput.ReadLine.Substring(employeeDataFileInput.ReadLine.IndexOf(":")))
End Sub

我的txt文件数据

Record Number: 1
First Name: Tim
Middle Name: K
Last Name: Black
Employee Number: 111
Phone Number: 1112223344
Extention: 1
Email: sv@gmail.com
Department: Accounting

Record Number: 2
First Name: Jim
Middle Name: K
Last Name: Fisher
Employee Number: 222
Phone Number: 2223334455
Extention: 2
Email: jf@gmail.com
Department: Administration

Record Number: 3
First Name: James
Middle Name: T
Last Name: Holt
Employee Number: 333
Phone Number: 3334445566
Extention: 3
Email: jh@gmail.com
Department: Sales

2 个答案:

答案 0 :(得分:0)

您可以使用String的Split方法将行拆分为键/值对。

我的意思是:你读完整行并将其拆分。

答案 1 :(得分:0)

提示:这是一个读取文件并显示从您输入的输入中读取的数据的示例。关键是使用split方法将输入分开“:”。

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO

Namespace Console_Work
    Class Program

        Private Shared Sub Main(args As String())
            Dim FileName As String = "C:\temp\MyIOFile.txt"
            If Not File.Exists(FileName) Then
                Console.WriteLine("*** Error {0} does not exist. Please hit any keu", FileName)
                Console.Read()
                Return
            End If
            Dim inputRecord As [String]
            Using sr As StreamReader = File.OpenText(FileName)

                While (InlineAssignHelper(inputRecord, sr.ReadLine())) IsNot Nothing
                    'Process read record
                    ProcessInputRecord(inputRecord)
                End While
                Console.WriteLine("The end of the stream has been reached.")
            End Using
            Console.WriteLine("End - Please hit any key")
            Console.Read()
        End Sub

        '------------------------------------------------------------------------
        Public Shared Sub ProcessInputRecord(parm_record As String)

            Dim fldValue As String() = New String(0) {}
            Try
                fldValue = parm_record.Split(New [Char]() {":"C})
                If fldValue.GetUpperBound(0) = 1 Then
                    'the header text will be in fldvalue[0] and the data will be in fldValue[1]
                    Console.WriteLine("Header=" & fldValue(0) & vbTab & vbTab & "Data=" & fldValue(1))
                        'logic comes here when there is no :
                Else
                                '//logic comes here when there is no : in the input line
                End If
            Catch ex As Exception

                Console.WriteLine("***Error parsing:" & ex.Message & Environment.NewLine & parm_record)
            End Try

        End Sub
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function



    End Class
End Namespace