如何使用C#读取文本文件时删除空行

如何使用C#读取文本文件时删除空行

问题描述:

我有文本文件,并使用流读取器,当我的文件是有伴随数据的空行读它,它不读取任何东西。如何使用C#删除空行。

i have text file and read it using stream reader, when my file is having empty line along with data, it does not read any thing. how to remove the empty line using C#.

那么,你应该从StreamReader的在使用的方法的ReadLine()一个循环。当您收到一个空行,只需检查)从的ReadLine(获得的字符串是空的。如果是,则忽略该行。
尝试是这样的:

Well, you should use the method "ReadLine()" from the StreamReader in a loop. When you receive an empty line, simply check if the string obtained from ReadLine() is empty. If it is, ignore the line. Try something like:

    StreamReader input = new StreamReader("...");
    String line = null;

    do 
    {
    line = input.ReadLine();

    if (line == null)
    {
    return;
    }

    if (line == String.Empty)
    {
    continue;
    }

    // Here you process the non-empty line

    } while (true);