在文件中查找字符串的最快方法 [英] Fastest way to find strings in a file

查看:61
本文介绍了在文件中查找字符串的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不超过10KB的日志文件(文件大小最多可以达到2 MB),我想确定文件中是否至少出现了一组这样的字符串.这些字符串将位于不同的行上,例如

I have a log file that is not more than 10KB (File size can go up to 2 MB max) and I want to find if atleast one group of these strings occurs in the files. These strings will be on different lines like,

ACTION:.......

ACTION:.......

输入:...........

INPUT:...........

结果:..........

RESULT:..........

我至少需要知道文件中是否存在上述一组.我已经进行了大约100次测试(每次日志都不一样,所以我需要重新加载并读取日志),所以我正在寻找最快且更合理的方法.

I need to know atleast if one group of above exists in the file. And I have do this about 100 times for a test (each time log is different, so I have reload and read the log), so I am looking for fastest and bets way to do this.

我在论坛中查找了最快的方法,但我认为对于这些解决方案来说,我的文件太大.

I looked up in the forums for finding the fastest way, but I dont think my file is too big for those silutions.

感谢.

推荐答案

我会逐行阅读并检查条件.看到一个小组后,您可以退出.这样,您无需将整个文件读入内存.像这样:

I would read it line by line and check the conditions. Once you have seen a group you can quit. This way you don't need to read the whole file into memory. Like this:

    public bool ContainsGroup(string file)
    {
        using (var reader = new StreamReader(file))
        {
            var hasAction = false;
            var hasInput = false;
            var hasResult = false;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (!hasAction)
                {
                    if (line.StartsWith("ACTION:"))
                        hasAction = true;
                }
                else if (!hasInput)
                {
                    if (line.StartsWith("INPUT:"))
                        hasInput = true;
                }
                else if (!hasResult)
                {
                    if (line.StartsWith("RESULT:"))
                        hasResult = true;
                }

                if (hasAction && hasInput && hasResult)
                    return true;
            }
            return false;
        }
    }

此代码检查是否有一行以ACTION开头,然后以INPUT开头,然后以RESULT开头.如果这些顺序不重要,则可以省略 if()else if()检查.如果该行不是以字符串开头,则将 StartsWith 替换为 Contains .

This code checks if there is a line starting with ACTION then one with INPUT and then one with RESULT. If the order of those is not important then you can omit the if () else if () checks. In case the line does not start with the strings replace StartsWith with Contains.

这篇关于在文件中查找字符串的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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