VB.net在.txt中搜索特定字符串

时间:2016-10-19 12:50:43

标签: vb.net vb.net-2010

我有一个文本文件,其中包含以下内容:

IDJ blablabla/blablabla: 745 6001230015108                  344 00
1234 6001230015108       blabla
IDJ blablabla/blablabla: 745 6001230015109                  344 00
1234 6001230015109       blabla
IDJ blablabla/blablabla: 745 6001230015107                  344 00
1234 6001230015107       blabla

我的问题是如何从这个文件中提取以下数字到文本文件中:

6001230015108                  
6001230015109 
6001230015107        

我有以下代码,但它的作用是提取整行并重复。

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'read info from the file'
    Dim objReader As New System.IO.StreamReader("D:\input.txt")
    Dim objWriter As New System.IO.StreamWriter("D:\output.txt")
    Dim strTextFileInfo() As String 'an array of strings for each line'
    Dim arrCounter As Integer = 0   'counter for the array'
    Dim strTextToSearch As String = String.Empty    'text to search'

    'read all lines'
    Do While objReader.Peek <> -1
        'redim the array with the needet elements'
        ReDim Preserve strTextFileInfo(arrCounter)
        'input the info'
        strTextFileInfo(arrCounter) = objReader.ReadLine
        'increase the elements'
        arrCounter = arrCounter + 1
    Loop
    'enter the text to search'
    strTextToSearch = InputBox("Enter text to search")
    'search all elements for the string'
    For i As Integer = 0 To arrCounter - 1
        If strTextFileInfo(i).ToLower.Contains(strTextToSearch.ToLower) Then
            'display the found elements'
            objWriter.WriteLine(strTextFileInfo(i))
            'MessageBox.Show(strTextFileInfo(i))
        End If
    Next
End Sub

感谢您的时间。

0 个答案:

没有答案