Jump to content

[C#] Finding exact match in text file

Drown

Hello everyone,

 

I have a text file that contains a bunch of information on some persons, username, password, name,  adress, phone number, etc.

I'm asking the user to enter his username and password, and then I check with the text file to see if it's there.

 

This is what I'm using :

string username = textBox_username.Text;string password = textBox_password.Text;string path = "C:\\test\\infos.txt";StreamReader sr = new StreamReader(path);while (sr.Peek() > -1){   string infos = sr.ReadLine();   if (infos.Contains(username))   {      if (infos.Contains(password))      MessageBox.Show("Found!");      else      {       MessageBox.Show("Wrong password");      }   }   else    MessageBox.Show("Invalid username ");  }

(This isn't a copy&paste, I simplified it for this topic, I hope the synthax is correct)

 

It's working fine as long as the user is honest, and enter a complete username. My problem is that with Contains, if there is a user Joey in the list and someone else's username is Joe, Contrains("Joe") will return true once the reader read the line Joey.

 

Is there any way to find an exact match, so that only "Joe" will return true with Contains and not "Joey" also?

I've looked at REGEX, however I have no idea how to set them up properly when using a variable for the verification (Contains(username) and not Contains("Joe"))

 

Thanks for the help!

 

Link to comment
Share on other sites

Link to post
Share on other sites

How is the data stored? CSV?

 

Yes, each line contains information about a specific user.

 

Something like John Doe, 0001, password123

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, each line contains information about a specific user.

 

Something like John Doe, 0001, password123

 

Separate the line into an array and just check the items within.

Link to comment
Share on other sites

Link to post
Share on other sites

Hello everyone,

 

I have a text file that contains a bunch of information on some persons, username, password, name,  adress, phone number, etc.

I'm asking the user to enter his username and password, and then I check with the text file to see if it's there.

 

This is what I'm using :

string username = textBox_username.Text;string password = textBox_password.Text;string path = "C:\\test\\infos.txt";StreamReader sr = new StreamReader(path);while (sr.Peek() > -1){   string infos = sr.ReadLine();   if (infos.Contains(username))   {      if (infos.Contains(password))      MessageBox.Show("Found!");      else      {       MessageBox.Show("Wrong password");      }   }   else    MessageBox.Show("Invalid username ");  }

(This isn't a copy&paste, I simplified it for this topic, I hope the synthax is correct)

 

It's working fine as long as the user is honest, and enter a complete username. My problem is that with Contains, if there is a user Joey in the list and someone else's username is Joe, Contrains("Joe") will return true once the reader read the line Joey.

 

Is there any way to find an exact match, so that only "Joe" will return true with Contains and not "Joey" also?

I've looked at REGEX, however I have no idea how to set them up properly when using a variable for the verification (Contains(username) and not Contains("Joe"))

 

Thanks for the help!

 

Don't use contains, use ==....or if you really feel like it .Equals().  The == is actually implemented properly in C# (unlike Java...never use == in Java strings).

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

What wandering fool said. You can simply use the == to compare two strings in C# and C+ which only returns true if both strings are exact match.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×