Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
private void button1_Click(object sender, EventArgs e)
    {
        string line, lastchar;
        // Read the file and display it line by line.
        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();
    }
Posted
Updated 5-Dec-16 4:57am
v2
Comments
PIEBALDconsult 4-Dec-16 16:20pm    
Try String.Split

You know how to check last char
to find the word:
-you need to find the first digit after second place in string with a loop
-then once you know where the word ends, it should be easy to extract the word.

You can also resort to RegEx (Regular Expressions)

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
v2
You have to learn:
1. How to formulate Regex patterns, Google will find your many online tutorials.
2. How to implement Regex in C#, ask Regex Class (System.Text.RegularExpressions)[^]
Study the following example and adapt it to your requirement:
C#
using System;
using System.Text.RegularExpressions;

public class Program
{
	public static void Main()
	{
		const string pattern = @"(?<=\d)[a-zA-Z]+(?=\d{3}[.]\d{3}[.]\d{3}-.+2$)";
		string[] users = new string[5]
		{
			"1Paulo111.111.111-11addaqwe2",
			"2Rambo425.433-628-43ererssd3",
			"1Momba111.111.111-11asdsad4432",
			"1Mauricio111.111.111-22wwcssfd2",
			"1Saulo111.111.111-11qwe1231231"
		};

		foreach (string user in users)
		{
			Match m = Regex.Match(user, pattern);
			if(m.Success) Console.WriteLine(m.Value);
		}
	}
}

Try it out online here Home | .NET Fiddle[^]
 
Share this answer
 
C#
string line, lastchar;
       // Read the file and display it line by line.
       System.IO.StreamReader file = new System.IO.StreamReader("a.txt");
       while ((line = file.ReadLine()) != null)
       {
           lastchar= line.Substring(line.Length - 1, 1);
           if (lastchar.Equals("2"))
           {
               var splitString = line.Split('.');

   string nameToPrint = splitString[0].Substring(1, splitString[0].Length - 4);           
 MessageBox.Show("Prints the name of the user here: "+ nameToPrint );
           }


       }
       file.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900