Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify that string array contains a certain string

Tags:

arrays

vb.net

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!

like image 688
user3108663 Avatar asked Oct 16 '25 04:10

user3108663


1 Answers

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
like image 165
Moumit Avatar answered Oct 18 '25 04:10

Moumit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!