문제

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