vb.net 比较字符串与数组中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16218804/
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 13:23:53  来源:igfitidea点击:

Compare String with Strings in array

vb.net

提问by Paul B

I'm trying to use this script to compare a users input from a text box with the 22 correct words. I'm not looking for multiple cases, such as VICEis in ADVICEso it would be 2 values; I want it to have the string values to accept only equal values.

我正在尝试使用此脚本将用户从文本框中输入的内容与 22 个正确的单词进行比较。我不是在寻找多种情况,例如VICEinADVICE所以它是 2 个值;我希望它的字符串值只接受相等的值

At the moment, it is only recognizing the first word TIEDand displays a message box "found", but it doesn't not recognize any other word in the list.

目前,它仅识别第一个单词TIED并显示“找到”消息框,但无法识别列表中的任何其他单词。

I am writing in visual basic script

我正在用可视化基本脚本编写

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

回答by Dan Puzey

The problem is that your loop is explicitly returning if the first item isn't a match. You only know you don't have a match if your loop completes without finding one, so try something like this instead:

问题是,如果第一项不匹配,您的循环将显式返回。如果您的循环完成而没有找到匹配项,您只知道您没有匹配项,因此请尝试以下操作:

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")

This will only display "incorrect word" if allof the items in your list fail the first test.

如果列表中的所有项目都未通过第一次测试,这只会显示“错误的单词” 。

回答by Pandian

Try like below, It will help you...

像下面这样尝试,它会帮助你......

Sample :

样本 :

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

Full Code :

完整代码:

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

回答by Siddharth Rout

Why STRCOMP? Why not a direct comparision if you want an exact match?

为什么STRCOMP?如果您想要完全匹配,为什么不直接比较?

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

回答by tymeJV

I would use a for loop, something like

我会使用 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