如何使用C#读取文本文件时删除空行 [英] how to remove empty line when reading text file using C#

查看:2571
本文介绍了如何使用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);

这篇关于如何使用C#读取文本文件时删除空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆