使用streamreader读取包含此“//”的行?

发布于 2024-08-09 07:02:28 字数 65 浏览 5 评论 0原文

读取任何以“//”开头的行的文本文件,忽略此行并移至下一行。 输入文本文件具有一些单独的分区。逐行查找进程和此标记。

Read a Text file having any line starts from "//" omit this line and moved to next line.
The Input text file having some seprate partitions. Find line by line process and this mark.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-08-16 07:02:28

如果您使用 .Net 3.5,则可以将 LINQ 与包裹在 Stream Reader 中的 IEnumerable 一起使用。如果您可以使用 where 语句来归档 statmens,或者更好的是使用带有正则表达式的 select 来修剪注释并将数据保留在同一行上,那么这个很酷的部分。

//.Net 3.5
static class Program
{
    static void Main(string[] args)
    {
        var clean = from line in args[0].ReadAsLines()
                    let trimmed = line.Trim()
                    where !trimmed.StartsWith("//")
                    select line;
    }
    static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}

...

//.Net 2.0
static class Program
{
    static void Main(string[] args)
    {
        var clean = FilteredLines(args[0]);
    }
    static IEnumerable<string> FilteredLines(string filename)
    {
        foreach (var line in ReadAsLines(filename))
            if (line.TrimStart().StartsWith("//"))
                yield return line;
    }
    static IEnumerable<string> ReadAsLines(string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}

If you are using .Net 3.5 you can use LINQ with a IEnumerable wrapped around a Stream Reader. This cool part if then you can just use a where statement to file statmens or better yet use a select with a regular expression to just trim the comment and leave data on the same line.

//.Net 3.5
static class Program
{
    static void Main(string[] args)
    {
        var clean = from line in args[0].ReadAsLines()
                    let trimmed = line.Trim()
                    where !trimmed.StartsWith("//")
                    select line;
    }
    static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}

...

//.Net 2.0
static class Program
{
    static void Main(string[] args)
    {
        var clean = FilteredLines(args[0]);
    }
    static IEnumerable<string> FilteredLines(string filename)
    {
        foreach (var line in ReadAsLines(filename))
            if (line.TrimStart().StartsWith("//"))
                yield return line;
    }
    static IEnumerable<string> ReadAsLines(string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}
奶气 2024-08-16 07:02:28

我不确定您到底需要什么,但是,如果您只想从流中的某些文本中过滤 // 行...只需记住在使用后关闭流即可。

public string FilterComments(System.IO.Stream stream)
        {
            var data = new System.Text.StringBuilder();
            using (var reader = new System.IO.StreamReader(stream))
            {
                var line = string.Empty;
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    if (!line.TrimStart(' ').StartsWith("//"))
                    {
                        data.Append(line);
                    }
                }
            }

            return data.ToString();
        }

I'm not sure what you exactly need but, if you just want to filter out // lines from some text in a stream... just remember to close the stream after using it.

public string FilterComments(System.IO.Stream stream)
        {
            var data = new System.Text.StringBuilder();
            using (var reader = new System.IO.StreamReader(stream))
            {
                var line = string.Empty;
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    if (!line.TrimStart(' ').StartsWith("//"))
                    {
                        data.Append(line);
                    }
                }
            }

            return data.ToString();
        }
灯下孤影 2024-08-16 07:02:28
Class SplLineIgnorStrmReader:StreamReader  // derived class from StreamReader

SplLineIgnorStrmReader ConverterDefFileReadStream = null;
{
//created the Obj for this Class.
Obj = new SplLineIgnorStrmReader(strFile, Encoding.default);
}

public override string ReadLine()
        {
            string strLineText = "", strTemp;
            while (!EndOfStream)
            {
                strLineText = base.ReadLine();
                strLineText = strLineText.TrimStart(' ');
                strLineText = strLineText.TrimEnd(' ');
                strTemp = strLineText.Substring(0, 2);
                if (strTemp == "//")
                    continue;
                break;

            }
            return strLineText;

这是如果您想读取文本文件并省略该文件中的任何注释(此处排除“//”注释)。

Class SplLineIgnorStrmReader:StreamReader  // derived class from StreamReader

SplLineIgnorStrmReader ConverterDefFileReadStream = null;
{
//created the Obj for this Class.
Obj = new SplLineIgnorStrmReader(strFile, Encoding.default);
}

public override string ReadLine()
        {
            string strLineText = "", strTemp;
            while (!EndOfStream)
            {
                strLineText = base.ReadLine();
                strLineText = strLineText.TrimStart(' ');
                strLineText = strLineText.TrimEnd(' ');
                strTemp = strLineText.Substring(0, 2);
                if (strTemp == "//")
                    continue;
                break;

            }
            return strLineText;

This is if u want to read the Text file and omit any comments from that file(here exclude "//" comment).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文