嘿所以我有一个文本框,我想比较用户在文本框中键入的内容与数组中来自文本文件的单词。所以我已经将用户文本作为Dim回答作为字符串,并且我试图查看答案是否在数组中加载的单词列表中。这就是我现在所拥有的,我只是想使用arrayname.contains,但我得到错误"'包含'不是form1.words"的成员。请帮助。单词是数组名称btw。
starmap
答案 0 :(得分:1)
我将列出两种不同的方法
ContainsWord1()使用:
Enumerable.Any(Of TSource) - .NET 3.5 +
参考:https://msdn.microsoft.com/en-us/library/yw84x8be(v=vs.110).aspx
ContainsWord2()使用:
Array.Exists(Of T)方法(T(),Predicate(Of T)) - .NET 2.0 +
参考:https://msdn.microsoft.com/en-us/library/bb337697(v=vs.110).aspx
两者都不区分大小写。
Private Function ContainsWord1(p_wordsArray as string(), p_word As string) as Boolean
return p_wordsArray.Any(Function(s as String) s.Equals(p_word.trim, StringComparison.CurrentCultureIgnoreCase))
End Function
Private Function ContainsWord2(p_wordsArray as string(), p_word As string) as Boolean
return Array.Exists(p_wordsArray, Function(s As String) s.Equals(p_word.trim, StringComparison.CurrentCultureIgnoreCase))
End Function
private Sub ContainsWordExample()
Dim _words = New String(){"blue","red","yellow", "black"}
Dim _yellow as string = "YeLloW"
Dim _green as string = "green"
'Yellow test
If ContainsWord1(_words, _yellow) then
messagebox.Show("You got it!")
Else
messagebox.Show("Try again!")
End If
If ContainsWord2(_words, _yellow) then
messagebox.Show("You got it!")
Else
messagebox.Show("Try again!")
End If
'Green test
If ContainsWord1(_words, _green) then
messagebox.Show("You got it!")
Else
messagebox.Show("Try again!")
End If
If ContainsWord2(_words, _green) then
messagebox.Show("You got it!")
Else
messagebox.Show("Try again!")
End If
End Sub