user3108663
user3108663

Reputation: 41

Verify that string array contains a certain string

I'm aware that this topic has already been covered a few times, but I was unable to find my answer on any of the associated posts.

I have a small array of three string items. When text is entered into the text box on my form and the "Verify" button is pressed, I should like to verify that the text typed into the text box can in fact be found in the array.

I have been attempting to use the .Contains method, but to no avail. It has only worked for the first item in the array. The others fail to be recognized.

My code is as follows:

Dim STRarray as string() = {"RUT","MB","PR"}

if STRarray.contains(textbox.text) Then
    messagebox.show("Item Found.")
else
    messagebox.show("Unable to Locate String.")
end if

Now as I stated above, if I type RUT into the textbox, the code works. However if I enter MB or PR it is unable to find them.

Any help would be appreciated. Thank you!

Upvotes: 4

Views: 20018

Answers (1)

Moumit
Moumit

Reputation: 9630

https://dotnetfiddle.net/Ks8SFQ ... it is working .. what you are missing

try Trim and ToUpper.. like below.. it may work

Dim STRarray as string() = {"RUT","MB","PR"}

if STRarray.contains(textbox.text.trim().ToUpper()) Then
    messagebox.show("Item Found.")
else
    messagebox.show("Unable to Locate String.")
end if

Upvotes: 3

Related Questions