从文本文件中读取某些字符串并将其输出到TextBox

时间:2015-03-04 18:43:14

标签: vb.net

我是使用Visual Studio 2013编程到Visual Basic的新手,我无法弄清楚如何从我正在阅读的文本文件中提取我想要的元素。我能够拉入整个文本文件我的代码如下,但我不需要整个文本文件。我只需要两件东西输出到我在TextBox中定义的字段。

Public Class Form1

    Private Sub BrowseBtn2_Click(sender As Object, e As EventArgs) Handles BrowseBtn2.Click
        OpenFileDialog2.ShowDialog()
        FilePathLabel2.Text = OpenFileDialog2.FileName
    End Sub

    Private Sub GenerateBtn_Click(sender As Object, e As EventArgs) Handles GenerateBtn.Click

        Dim FILE_NAME As String = OpenFileDialog2.FileName
        Dim TextLine As String

        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(OpenFileDialog2.FileName)

            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine() & vbNewLine
            Loop
            TextBox1.Text = TextLine
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

    Private Sub CloseBtn_Click(sender As Object, e As EventArgs) Handles CloseBtn.Click
        Me.Close()
    End Sub
End Class

文本文件如下所示:

**

*Header

**
* YYY Vendor: MY

*

Customer: My Wifi

Quantity: 10

Type: 3456778

Profile: NNNN-1

Batch: 0123

*

UMTS Transport_key: 0

IMS Transport_key: 000

*

Address1:

Address2:

Address3:

Address4:

*

Graph_ref:

PO_ref_number:

*

*

*

*

Network Selection Parameters Input File (NSPIF) Version: 00.00.0000

*

GSM SMSC Address: 18001234567

Email Gateway address: 000

*

*

*

*

**

*Input Vars

**

var_in_list:

IMSI:310000000000001

Ser_nb:89010000000000000001

IMPI:310111111111111@private.net

IMPU:310111111111111@one.net

**

*Output Variables

**

var_out: PIN1/PUK1/PIN2/PUK2/K_UMTS/K_IMS/ADM1/Access_Control/DL_Key

310000000000001 89010000000000000001 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000002 89010000000000000002 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000003 89010000000000000003 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000004 89010000000000000004 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000005 89010000000000000005 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000006 89010000000000000006 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000007 89010000000000000007 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000008 89010000000000000008 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000009 89010000000000000009 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF
310000000000010 89010000000000000010 2222 11111111 3333 22222222 00112233445566778899AABBCCDDEEFF 00112233445566778899AABBCCDDEEFF 1111111111111111 0000 00112233445566778899AABBCCDDEEFF

我想要的是从第一行以“310000000000001”开头的字符串“89010000000000000001”和以“310000000000010”开头的最后一行的字符串“89010000000000000010”

我应该如何获得这两个字符串?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该首先将行存储在变量

Do While objReader.Peek() <> -1
    Dim curLine As String = objReader.ReadLine()
    TextLine = TextLine & curLine & vbNewLine
Loop

然后只打印以310000000000001开头的行

Do While objReader.Peek() <> -1
    Dim curLine As String = objReader.ReadLine()

    If curLine.StartsWith("310000000000001") Then
        TextLine = TextLine & curLine & vbNewLine
    End If
Loop

然后,您可以存储第一行和最后一行,并在流程结束时打印它们

Dim startLine, endLine As String

startLine = ""
endLine = ""

Do While objReader.Peek() <> -1
    Dim curLine As String = objReader.ReadLine()

    If curLine.StartsWith("310000000000001") Then
        If startLine = "" Then
            startLine = curLine
        End If

        endLine = curLine
    End If
Loop

TextLine = TextLine & startLine & vbNewLine
TextLine = TextLine & endLine & vbNewLine

使用简单的分割,您可以获得该行的第二项。

TextLine = TextLine & startLine.Split(" ")(1) & vbNewLine
TextLine = TextLine & endLine.Split(" ")(1) & vbNewLine