I have a text file structured like this:
Time: 5:31
.
.
Time: 5:50
.
.
Time: 6:30
.
.
Time: 7:30
For this, I am trying to obtain the last two instances of "Time: ", which would be:
Time: 6:30
.
.
Time: 7:30
I already know how to obtain the last instance, by way of my code below:
string end_time = "";
string start_time = "";
string file = @"mypath.txt";
foreach(string line in File.ReadAllLines(file))
{
end_time = Endtime_value(line, end_time);
}
Console.WriteLine(end_time);
}
public static string Endtime_value(string line, string end_time)
{
if (line.Contains("Time"))
{
end_time = line;
}
return end_time;
}
In the above I could also return the last line of the text file, as it contains the last instance of "Time: "
However, for this, I am also trying to create a function to return the second to last instance of "Time: " which would be 6:30 and am not trying to do it in a way where it deletes the last line as well.
Is there a way to somehow implement a function to ignore the last line of a text file? (my last line of the file is the last instance of "Time: ") I was planning on doing it as so:
public static string Starttime_value(string line, string start_time,string file)
{
//function here to ignore the last line of a text file
//then apply the same logic of End_Time function to get second to the last instance of "Time: "
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…