Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 将字符串与数组中的字符串进行比较_Vb.net - Fatal编程技术网

Vb.net 将字符串与数组中的字符串进行比较

Vb.net 将字符串与数组中的字符串进行比较,vb.net,Vb.net,我试图用这个脚本将用户从文本框输入的内容和22个正确的单词进行比较。我没有寻找多个案例,例如VICE在ADVICE中,因此它将是2个值我希望它的字符串值只接受相等的值 目前,它只识别第一个单词TIED,并显示一个消息框“found”,但不识别列表中的任何其他单词 我正在用visualbasic脚本写作 Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt

我试图用这个脚本将用户从文本框输入的内容和22个正确的单词进行比较。我没有寻找多个案例,例如
VICE
ADVICE
中,因此它将是2个值我希望它的字符串值只接受相等的值

目前,它只识别第一个单词
TIED
,并显示一个消息框“found”,但不识别列表中的任何其他单词

我正在用visualbasic脚本写作

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
    Dim Find As String = userinput
    For Each Str As String In StrCorrect
        If StrComp(Str, userinput, CompareMethod.Text).ToString = 0 Then
            MsgBox("Found" & userinput)
            Return
        Else : MsgBox("incorrect word")
            Return
        End If
    Next
End Sub

为什么是STRCOMP?如果你想要精确的匹配,为什么不直接比较呢

    For Each Str As String In StrCorrect
        If Str = Find Then
            MessageBox.Show("Found :" & Str)
        End If
    Next

我会使用for循环,类似于

For i As Integer = 0 To StrCorrect.Length - 1
        If StrCorrect(i) = Find Then
            MsgBox("Found" & Find)
            Return
        'End if

        'The else statement simply alerting that it didnt find the right word on this iteration
        'The else can be removed if you dont want this alert
        Else
            MsgBox("incorrect word")
            'Return
        End If
Next

问题是,如果第一项不匹配,循环将显式返回。只有在循环完成但未找到匹配项时,您才知道没有匹配项,因此请尝试以下方法:

For Each Str As String In StrCorrect
    If StrComp(Str, userinput, CompareMethod.Text).ToString = 0 Then
        MsgBox("Found" & userinput)
        Return
    End If
Next

MsgBox("incorrect word")

如果列表中的所有项目都未通过第一次测试,则仅显示“不正确的单词”。

尝试下面的方法,它将帮助您

示例:

Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If
Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
Dim Find As String = userinput
Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If
完整代码:

Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If
Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
Dim Find As String = userinput
Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If

你可能想提到你写这篇文章的语言
visual studio
不是一种语言。我的VB有点生疏,但你不想用括号代替大括号吗?
我希望它的字符串值只接受相等的值。
相等的值并不意味着
VICE
=
VICE
非常有用,但不能解决问题中所述的问题:它只识别第一个单词“TIED”。还值得注意的是,使用
=
忽略了可能需要的任何区域性和区分大小写选项-通常更好的做法是使用
字符串
类的方法进行这些操作。它只应该识别精确匹配,而不是
建议
请参见问题
中的这一点,我希望它具有字符串值只接受相等的值。
这根本不会改变问题的结果。它在整个数组中循环,声明错误单词的MsgBox只是发出警报…一旦找到正确的单词它就会停止…非常感谢,这个答案成功了!