I have these text files as structured:
value1 value2 value3
Table1 Table2 Table3
(data set values here)
.
.
.(represent text in between)
.
value1 value2 value3
Table1 Table2 Table3
(data set values here)
and
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)
As they are here, they are structured differently, with some having two Table sets, some having three, and others having 4. However, they all share a common characteristic that that they all have the same formatting of how the table is structured:
value1 value2 value3
Table1 Table2 Table3
(data set values here)
What I want to do is that I want to be able to skip to the last occurrence of each data set at the bottom, regardless of the number of lines in the file, which is the one shown above. How would I be able to detect the last values at the vary end?
What I have so far is:
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
}
I think I would have to use the LastIndexOf Method, then specify those values in the parameters, then use it in conjunction with the for loop but I am unsure how to implement it so it would detect it.
EDIT
Input of sample text files:
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
What I want to be able to is detect the last occurence of Time and EndTime, so when the program is run it will return:
//for the first one
Time: 7:30am
EndTime: 8:30pm
//for the second one
Time: 3:30am
EndTime: 4:30pm
The simplest way to do the following:
would be something like this:
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);