4

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!

2
  • 1
    Did you try seeing the contents of STRarray? Commented Jan 19, 2016 at 16:26
  • 1
    Yes I have done that to make sure that my array is in fact populated. Thats why I'm so confused haha. Commented Jan 19, 2016 at 16:38

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

I'll try this, thank you! I already defaulted all of the text ToUpper through the text box, but I'll give the trim a tr.
Trim and ToUpper create unnecessary strings in memory. Use the overload of Contains that allows you to specify a case insensitive comparison: STRarray.Contains(textbox.Text, StringComparer.InvariantCultureIgnoreCase)
whats the different between StringComparer.CurrentCultureIgnoreCase and StringComparer.InvariantCultureIgnoreCase ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.