In visual C#, the button I am implementing needs to read a .txt file and check if each line on the text file ends with a certain character, and if it does, it takes the name on that line and prints it to a message box. So far, I have managed to make the condition to check wheter or not the specified character exists at the end of the line, but can't get the name on it, as it is between two sets of numbers. The name is right after the first character on the line and just before a set of numbers start, as they are the ID of the user.
And this is the text file:
1Paulo111.111.111-11addaqwe2
2Rambo425.433-628-43ererssd3
1Momba111.111.111-11asdsad4432
1Mauricio111.111.111-22wwcssfd2
1Saulo111.111.111-11qwe1231231
So the button needs to check if the current line ends with '2' and prints the name in the line. The name in the first line, for example, is Paulo, and as it ends with "2", "Paulo" would be printed to the messagebox, just as the third and the fourth line. Otherwise, it would skip to the next line. It would then be printed in the messagebox: "Paulo, Momba, Mauricio."
How can I do this?
What I have tried:
This is my code so far inside the button:
private void button1_Click(object sender, EventArgs e)
{
string line, lastchar;
System.IO.StreamReader file = new System.IO.StreamReader(@"rato.txt");
while ((line = file.ReadLine()) != null)
{
lastchar= line.Substring(line.Length - 1, 1);
if (lastchar== "2") MessageBox.Show("Prints the name of the user here");
}
file.Close();
}