2010-02-01 49 views
1

這可能不是很難做到。 但我有兩個文本文件。第一個獲得了某些關鍵字的列表。 每一個如果這些關鍵字被送入組合框。vb.net - 從某個字符串搜索文本文件,從該點讀取

現在,當我從該組合框中選擇一個項目時,我想搜索其他文本文件中的同一個關鍵字並從該點讀取到下一個關鍵字。然後將每行添加到列表框中。希望我能讓自己明白。

也許我應該使用INI文件來達到這個目的,對我來說並不重要。

該文本文件具有這種結構; Textfile1:

London 
Oslo 
New York 
Hamburg 
Amsterdam 

第二文本文件具有這種結構; Textfile2:

'London' 
Apples 
Oranges 
Pears 

'Oslo' 
Pasta 
Salami 
Monkeyballs 

'New York' 
Dada 
Duda 
Dadadish 

等等等等

這可能嗎?我想這樣做的原因是創建一個完全動態的系統。一個完全依靠任何信息存儲在這些文本文件中的人。以後我會從這些結果中構建相當複雜的字符串。

到目前爲止,我有這個閱讀的第一個文件和每行添加到組合框:

Dim oReadMenuFunction as System.IO.StreamReader 
oReadMenuFunction = IO.File.OpenText("textfile1.txt") 

Do While oReadMenuFunction.Peek <> -1 
    Dim LineIn as String = oReadMenuFunction.ReadLine() 
    Combobox.Items.Add(LineIn) 
Loop 

回答

1

這裏是你可以用它來分析你的第二個示例文件的功能。它需要一個關鍵字作爲參數,返回相關的項目:

Function ReadData(ByRef keyword As String) As IEnumerable(Of String) 
    Dim result = New List(Of String) 
    Using reader = New StreamReader("file2.txt") 
     Dim line As String = reader.ReadLine() 
     Dim take = False 
     Do While line IsNot Nothing 
      If line.StartsWith("'") Then 
       take = False 
      End If 
      If String.Equals("'" + keyword + "'", line) Then 
       take = True 
      End If 
      If take And Not String.IsNullOrEmpty(line) And Not line.StartsWith("'") Then 
       result.Add(line) 
      End If 
      line = reader.ReadLine() 
     Loop 
    End Using 
    Return result 
End Function 

並且你使用這樣的:

Dim items = ReadData("London") 
+0

感謝名單!我不得不重做一些,但主體結構像魅力一樣工作! :) – 2010-02-01 12:49:57

相關問題