Is there a way to find an exact match in a string?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
I am using: If Login.Contains(My.Settings.LastLogin) with 2 different strings, one has the string "Mike" and the other is "Mike2". Since they both have "Mike" in the string, it is obviously passing the test each time.

However, I need to only have it match on an exact search, so if I am searching for "Mike", it won't pass for "Mike2" (I am using it to help me decide which password to parse out of a login string).

TIA
 
Mabey I'm missing something but why wouldn't you just check:

If Login = My.Settings.LastLogin

?

Or perhaps if necessary, If Login.ToString = ...

Seems like I'm over simplifying it though...
 
@Raven:
Sorry, I forgot to mention that the "Login" string itself is in the form of Mike;MikePW, and then the next one in line for checking would be Mike2;MikePW2, Mike3;MikePW3, and so forth. And then the My.Settings.LastLogin is just the first part of that string. For example: "Mike"

Then I plan on just taking the name "Mike" and looping through the logins for that login name, and parsing out the password after the ";" for use as the PW.

Here is the actual code I have so far. it works great with the exception of similar names for the login:

VB.NET:
Dim strLogins() As String = Split(My.Settings.Logins, "|") ' This is one long string in the format of: login;pw|login;pw|

If My.Settings.LastLogin.Length > 0 Then

                For Each Login As String In strLogins
                    ' ## Problem here with Contains. "Mike" is in both login names, so it's passing the test all times (i.e. if login name also is Mike1, Mike 2, Mike3, etc). It should only pass when exact.
                    If Login.Length > 0 AndAlso Login.Contains(My.Settings.LastLogin) Then
                        ' Parse the Logins/PWs and when found, add to the Login/PW text boxes
                        ToolStripTextBoxLogin.Text = Login.Substring(0, Login.IndexOf(";"))
                        ToolStripTextBoxPassword.Text = Login.Substring(Login.IndexOf(";") + 1)
                        ToolStripComboBoxLoginList.Text = My.Settings.LastLogin
                    End If
                Next

End If

TIA
 
Ok, I figured it out. I just learned about the Like operator. :p

I changed it to:
VB.NET:
If Login.Length > 0 AndAlso Login.Substring(0, Login.IndexOf(";")) Like My.Settings.LastLogin Then

And it's working great. :)
 
Back
Top