2017-04-06 21 views
-6

我有一個StreamReader reader在那裏我有內容如下
抓取線列表在C#

一些文字在這裏在這裏
測試文字
TEST_START ,,,,,,
試驗中,如圖1所示,文本
測試,2,文本
試驗,如圖3所示,文本
測試,4,文本
試驗,如圖5所示,文本

TEST_STOP ,,,,,,
一些文本這裏

我需要在下面的代碼使用的list.I獲取TEST_STARTTEST_STOP之間的界限,但不知道我錯過了在這裏拍攝的東西
Reference

string start_token = "TEST_START"; 
string end_token = "TEST_STOP"; 
string line; 
bool inCorrectSection = false;  
while ((line = reader.ReadLine()) != null) 
{ 
    if (line.StartsWith(start_token)) 
    { 
     if (inCorrectSection) 
     { 
      break; 
     } 
     else if(line.StartsWith(end_token)) 
     {       
      inCorrectSection = true; 
     } 
    } 
    else if (inCorrectSection) 
     myList.Add(line); 
} 
+1

好了,那你試試? – BugFinder

+0

你的問題是什麼? –

+0

您是要求我們爲您編寫代碼還是您有一些代碼來顯示您試圖解決您的問題? – Steve

回答

1

它看起來像你只需要稍微更改邏輯:

  1. 當您找到起始行時,將您的變量設置爲true(並繼續循環)。
  2. 當您找到最後一行時,將您的變量設置爲false(並繼續循環,或者如果您只希望有一個段捕獲,則打破循環)。
  3. 如果你的變量爲真,捕捉線

例如:

while ((line = reader.ReadLine()) != null) 
{ 
    if (line.StartsWith(start_token)) 
    { 
     // We found our start line, so set "correct section" variable to true 
     inCorrectSection = true; 
     continue; 
    } 

    if (line.StartsWith(end_token)) 
    {       
     // We found our end line, so set "correct section" variable to false 
     inCorrectSection = false; 
     continue; // Change this to 'break' if you don't expect to capture more sections 
    } 

    if (inCorrectSection) 
    { 
     // We're in the correct section, so capture this line 
     myList.Add(line); 
    } 
} 
+0

謝謝Rufus,它的工作很好。我能夠得到這些線。 – SDLBeginner