using streamreader, if line starts with '0' skip the line???

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
        }
    }
}
有帮助吗?

解决方案

You can skip a line by just not doing anything with it. Quickest way, use continue;...

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

Using continue will effectively jump to the start of the while loop again, which will then proceed to reading the next line,

This is based on my understanding that you want to skip lines that start with "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"))
                {
                      continue;
                }
                else{
                      //Do stuff here
                }
          }
     }

Do something with the line only when it do not start with '0'

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top