C# .txt 文件:如果前一行包含 "X",则读取下一行的子字符串

C# .txt file: Reading a substring in next line if previous line contains "X"

我想从文本文件中读取,如果一行包含 "XYZ",我需要在该行中 return 个子字符串,然后读取下一行的另一个子字符串。

将有几行 return "XYZ" 并且在每一行中我都需要来自下一行的子字符串(每一行都是不同的值)。 目前我可以 return 带有 "XYZ" 的行中子字符串的所有实例,但是然后从第一个 return 下面的行中保留相同的子字符串(每次都应该是唯一的) =34=] 或者只是,如下所示,每个字符单独。

所以在下面的代码片段中,来自日志。如果一行包含 XYZ,那么我需要移至下一行并拉出 Date/Time/and 批次名称编号。 XYZ 将在整个日志中重复多次,每次都有不同的结果。

2015-07-02 11:03:13,838 [1] 信息位置 (null) (null) (null) – btnAction_Click, _HAH_Claim_T, [=24 的完成扫描=]XYZ

2015-07-02 11:03:14,432 [1] INFO Place (null) (null) (null) – btnAction_Click,设置批次名称1234567

string[] lines = File.ReadAllLines(@"Text Document.txt");


        foreach (string line in lines)
        {
            int success = line.IndexOf("XYZ");

            if (success > 0)
            {
                string pass = "Pass";
                string date = line.Substring(0, 10);
                string time = line.Substring(11, 12);
                int ID = line.LastIndexOf("XYZ");

                if (ID != 0)
                {
                    Console.WriteLine("\n\t" + pass + " Date: {0}\tTime: {1}", date, time);
                }

                string currentLine;
                string batchID;

                for (int i = 0; i < lines.Length; i++)
                {

                    currentLine = lines.Skip(6).First();

                    batchID = currentLine.Substring(100);
                    Console.WriteLine("\tBatchID{0}", batchID[i]);
                }

                Console.WriteLine();
            }



        }


        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
string[] lines = File.ReadAllLines(@"E:Sum.txt");
string str = string.Empty;
foreach (string line in lines)
{
    if (line.Contains("x"))
    {
        str += line;                   
        Console.WriteLine();
        continue;
    }
    break;
}

Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();

我会这样说?

string[] lines = File.ReadAllLines(@"Text Document.txt");

for(int i = 0; i < lines.Length(); i++){
    if(lines[i].Contains("XYZ")){
        string pass = "Pass";
        string date = line.Substring(0, 10);
        string time = line.Substring(11, 12);
        Console.WriteLine("\n\t" + pass + " Date: {0}\tTime: {1}", date, time);

        // Retrieve the value in the next line
        string nextLineAfterXYZ = lines[i + 1];
        batchID = nextLineAfterXYZ.Substring(100);
        Console.WriteLine("\tBatchID{0}", batchID);
    }
}

你应该添加一些错误处理,以防它是最后一行 (IndexOutOfBound) 等等

我不完全理解这个问题,因为它有点抽象,你想从每一行中提取什么,但像这样的东西有望让你走上正轨

    using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
    using (var reader = new StreamReader(stream))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            if (line.Contains("XYZ") && !reader.EndOfStream)
            {
                var nextLine = reader.ReadLine();
                var pass = "Pass";
                var date = nextLine.Substring(0, 10);
                var time = nextLine.Substring(11, 12);

                Console.WriteLine("\n\t" + pass + " Date: {0}\tTime: {1}", date, time);
            }
        }
    }

行变量是包含 XYZ 的行,下一行是它的下一行。如果这不符合您的要求,请将问题更新得更具体一些