我如何检测文本文件中最后两次出现的 characters/data 集#

How would I detect the last two occurrence of characters/data sets in a text file c#

我有这些结构化的文本文件:

value1 value2 value3          
Table1 Table2 Table3
(data set values here)
.
.
.(represent text in between)
.
value1 value2 value3
Table1 Table2 Table3            
(data set values here)          

value1 value2 value3          
Table1 Table2 Table3
(data set values here)
.
.
.(represent text in between)
.
value1 value2 value3
Table1 Table2 Table3            
(data set values here)
.
.
. 
value1 value2 value3
Table1 Table2 Table3            
(data set values here)

像这里一样,它们的结构不同,有的有两套Table,有的有三套,有的有四套。但是,它们都有一个共同的特点,就是它们都具有相同的特性table 结构的格式:

value1 value2 value3
Table1 Table2 Table3            
(data set values here)   

我想要做的是,我希望能够跳到底部每个数据集的最后一次出现,而不管文件中的行数,也就是上面显示的行数。我怎样才能检测到变化端的最后一个值?

我目前拥有的是:

string[] lines = File.ReadAllLines("myTextfile.txt"); //stores text file in array
foreach(var item in lines)
{
    Console.WriteLine(item.ToString()); //foreach loop here prints out code to console, line by line

}

我想我必须使用 LastIndexOf 方法,然后在参数中指定这些值,然后将它与 for 循环结合使用,但我不确定如何实现它以便它能够检测到它。

编辑

示例文本文件的输入:

Time: 5:30am         
EndTime: 5:30pm.
.
.(represent text in between)
.
Time: 7:30am         
EndTime: 8:30pm           

Time: 6:30am         
EndTime: 1:30pm 
.
.
.(represent text in between)
.
Time: 8:30am         
EndTime: 9:30pm
.
.
. 
Time: 3:30am         
EndTime: 4:30pm

我希望能够检测到 Time 和 EndTime 的最后一次出现,因此当程序为 运行 时,它将 return:

//for the first one 
Time: 7:30am         
EndTime: 8:30pm 
//for the second one 
Time: 3:30am         
EndTime: 4:30pm

执行以下操作的最简单方法:

  • 收集以 "Time:"
  • 开头的行的最后一次出现
  • 收集以 "EndLine:"
  • 开头的行的最后一次出现
  • 将两者输出为 "Time: xxx" 后跟 "EndTime: xxx",这两行

会是这样的:

string time = "";
string endTime = "";
foreach (string line in File.ReadLines("filename.txt")) // or any source of strings
    if (line.StartsWith("Time:"))
        time = line;
    else if (line.StartsWith("EndTime:"))
        endTime = line;
// error handling would go here, like what if time is still empty? or endTime?
Console.WriteLine(time);
Console.WriteLine(endTime);

我会在 SO 上建立文件 question/answers 的读取结尾。所以你可以读取一小段行缓冲区来得到最后的行。像这样:

    static async Task Main(string[] args)
    {
        using (var reader = new StreamReader("foo.txt"))
        {
            var (start, end) = DecomposeLastTwoTimeLines(reader);

            Console.WriteLine($"Start: {start}");
            Console.WriteLine($"End: {end}");
        }
    }

    static ValueTuple<string, string> DecomposeLastTwoTimeLines(StreamReader reader)
    {
        long TimeLinesLength = ("EndTime: 12:30pm".Length * 2 * 2) + 2;

        if (reader.BaseStream.Length > TimeLinesLength)
        {
            reader.BaseStream.Seek(-TimeLinesLength, SeekOrigin.End);
        }

        List<string> lines = new List<string>();

        string line;
        while ((line = reader.ReadLine()) != null)
            lines.Add(line);

        if (lines.Count < 2)
            throw new FileFormatException();

        return (lines[^2], lines[^1]);
    }