vb.net 验证字符串数组是否包含某个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34881773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:41:22  来源:igfitidea点击:

Verify that string array contains a certain string

arraysvb.net

提问by user3108663

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.

我一直在尝试使用 .Contains 方法,但无济于事。它仅适用于数组中的第一项。其他人无法识别。

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.

现在正如我上面所说的,如果我在文本框中输入 RUT,代码就可以工作。但是,如果我输入 MB 或 PR,则无法找到它们。

Any help would be appreciated. Thank you!

任何帮助,将不胜感激。谢谢!

回答by Moumit

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

https://dotnetfiddle.net/Ks8SFQ......它正在工作......你缺少什么

try Trimand ToUpper.. like below.. it may work

尝试TrimToUpper..如下所示..它可能会工作

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