【问题标题】:Getting the 2 lines above the current line in a text file C#获取文本文件 C# 中当前行上方的 2 行
【发布时间】:2020-07-12 20:46:22
【问题描述】:

我有一个 foreach 循环,对于每一行,我需要获取前 2 行并将它们存储在一个变量中。我该怎么做?我已经尝试了很长时间。

文件大小约5mb

我的 foreach 循环:

        foreach (string line in File.ReadAllLines("DeadByDaylightCopy.log"))
        {
            if (line.Contains("LogCustomization: --> TW"))
            {
                killer = "Wraith";
            }

            else if (line.Contains("LogCustomization: --> TR"))
            {
                killer = "Trapper";
            }

            else if (line.Contains("LogCustomization: --> HK"))
            {
                killer = "Spirit";
            }

            else if (line.Contains("LogCustomization: --> MK"))
            {
                killer = "Plague";
            }

            else if (line.Contains("LogCustomization: --> FK"))
            {
                killer = "Pig";
            }

            else if (line.Contains("LogCustomization: --> OK"))
            {
                killer = "GhostFace";
            }

            else if (line.Contains("LogCustomization: --> TN"))
            {
                killer = "Nurse";
            }

            else if (line.Contains("LogCustomization: --> KK"))
            {
                killer = "Legion";
            }

            else if (line.Contains("LogCustomization: --> BE"))
            {
                killer = "Huntress";
            }

            else if (line.Contains("LogCustomization: --> TC"))
            {
                killer = "Billy";
            }

            else if (line.Contains("LogCustomization: --> WI"))
            {
                killer = "Hag";
            }

            else if (line.Contains("LogCustomization: --> GK"))
            {
                killer = "Clown";
            }

            else if (line.Contains("LogCustomization: --> SD"))
            {
                killer = "Freddy";
            }

            else if (line.Contains("LogCustomization: --> DO"))
            {
                killer = "Doctor";
            }

            else if (line.Contains("LogCustomization: --> CA"))
            {
                killer = "Cannibal";
            }
            else if (line.Contains("LogCustomization: --> MM"))
            {
                killer = "Myers";
            }

            else if (line.Contains("LogCustomization: --> UK"))
            {
                killer = "Deathslinger";
            }

            else if (line.Contains("LogCustomization: --> SwedenKiller"))
            {
                killer = "Oni";
            }

            else if (line.Contains("LogCustomization: --> QK"))
            {
                killer = "Demogorgon";
            }

例如。文本文件

LogCustomization: --> TR_HEAD_03
LogCustomization: --> TR_TORSO_P01
LogCustomization: --> TR_LEGS_02
fsdfs
fsdfs
LogCustomization: --> KK_HEAD_04
LogCustomization: --> KK_BODY_P01
LogCustomization: --> KK_LEGS_P01
dfsdfs
LogCustomization: --> TW_HEAD02_LP01
LogCustomization: --> TW_BODY03_LP01
LogCustomization: --> TW_WEAPON07_P01
sfsdfs
LogCustomization: --> CM_HEAD_P01
LogCustomization: --> CM_TORSO_06 
LogCustomization: --> CM_LEGS_LP01
sdfsf
sdfsdf

看起来您的帖子主要是代码;请添加更多细节。 看起来您的帖子主要是代码;请添加更多详细信息。

【问题讨论】:

  • 将所有行存储在一个字符串数组中。遍历数组的循环以 i=0 开始。这样您就可以通过 arr[i-1]arr[i-2] 访问前两行
  • 底部的文字(“//2short //2short”等)是什么意思?
  • 你能举一个你试图阅读的文件的例子吗?循环中测试的关键字是否以 3 行间隔显示?
  • 这个文件有多大? ReadAllLine 从文件中加载所有内容,这可能对您的内存负载和性能非常重要
  • @Steve 帖子已编辑

标签: c# .net file


【解决方案1】:

我会在您的实际代码中更改很多内容来解决问题。

首先,将所有条件存储在像这样的字典中

Dictionary<string, string> keywords = new Dictionary<string, string>{
        {"LogCustomization: --> TW", "Wraith"},
        {"LogCustomization: --> TR", "Trapper"},
        {"LogCustomization: --> Sw", "Oni"},
        ... add the other keys here ....
    };

在字典中,您将要测试的所有条件存储为键,并将“SwedenKiller”这个词的特定词缩减为两个字母。在字典值中存储您检索该特定行时要使用的值。

此时你的代码可以减少很多

// Load all data in memory in the variable allData
var allData = File.ReadAllLines("");

// Loop using a normal loop. This will allow to use the variable x as an indexer
// to retrieve the lines before the current one....
for (int x = 0; x < allData.Length; x++)
{
    string line = allData[x];

    // Discard the lines that doesn't start with the LogCustomization words
    if (line.StartsWith("LogCustomization: --> "))
    {
        // Take the words and two more characters
        string sub = line.Substring(0, 22);

        // Check if the substring is listed in the keywords dictionary
        if (keywords.ContainsKey(sub))
        {
            // Take the value and ....
            string killer = keywords[sub];
            if (x > 0)
            {
                // Take the previous line (x-1)
                string prevLine = allData[x - 1];
                if (x > 1)
                {
                    // Take the previous previous line (x-2)
                    string prevprevLine = allData[x - 2];

                    // Here goes your real logic with kille e prev lines.
                    Console.WriteLine($"current={line}, prev={prevLine}, prevprev={prevprevLine}");
                }
            }
        }
        
    }
}

【讨论】:

    【解决方案2】:

    我不确定您要实现什么,这里是获取前两行的代码:

            var lines = File.ReadAllLines("DeadByDaylightCopy.log");
    
            for (int i = 0; i < lines.Length; i++)
            {
                var linesAnd2PreviousLines = lines[Math.Max(0, i - 2)..(i + 1)];
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 2013-05-31
      • 1970-01-01
      • 2011-01-12
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      相关资源
      最近更新 更多