2014-02-19 56 views
-4

使用streamreader,如果行以'0'開始跳過行???如何在流讀取器中跳過一行?

string FileToProcess = System.IO.File.ReadAllText(@"up.FolderPath"); 
using (StreamReader sr = new StreamReader(FileToProcess)) 
{ 
    while (!sr.EndOfStream) 
    { 
     string line = sr.ReadLine(); 
     if (line.StartsWith("0")) 
     { 
      line.Skip(); 
      // ????have to put a number line in here 
      // But i just want to skip the line it is on 
     } 
    } 
} 
+0

爲什麼不直接否定if? 'if(!line.StartsWith(「0」)){//用你所關心的行做東西}' – CodingIntrigue

+0

因爲我有其他的條件,想請教如何跳過當前行 – John

+0

創建一個int值? – John

回答

7

你可以跳過一行,只是不做任何事情。最快捷的方式,使用continue; ...

while (!sr.EndOfStream) 
{ 
    string line = sr.ReadLine(); 
    if (line.StartsWith("0")) 
    { 
     continue; 
    } 
    //process line logic 
} 

使用continue將有效再跳到while循環的開始,那麼這將繼續讀出下一行,

這是根據我的理解,你想跳過與"0"

-3

啓動線做一些與該行僅當它不「0」

+0

Wat ?!如何在讀取之前檢查它是否以「0」開頭? – musefan

+0

@musefan:我發佈的代碼並沒有經過測試..這只是一個想法 –

+0

它應該是顯而易見的,這個代碼將無法正常工作。 'line'還沒有分配給任何東西 – CodingIntrigue

1
string FileToProcess = System.IO.File.ReadAllText(@"up.FolderPath"); 
     using (StreamReader sr = new StreamReader(FileToProcess)) 
     { 
      while (!sr.EndOfStream) 
      { 
       string line = sr.ReadLine(); 
       if (line.StartsWith("0")) 
       { 
         continue; 
       } 
       else{ 
         //Do stuff here 
       } 
      } 
    } 
啓動
相關問題