Вопрос

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