尝试制作一个程序在另一个输入中搜索输入的单词

Trying to make a program that searches for an inputted word in another input

 Sub Button1Click(sender As Object, e As EventArgs)
    Dim words As Int64=0
    Dim dictionary As String()
    Dim Input1 As String
    Dim CurrentWord As String
    dictionary=New String() {}
    Input1=richTextBox1.text + (" ")
    Dim n As Int64=5

    For c = 1 To Len(Input1)
        If Mid(Input1,c,1) <> (" ") Then
            CurrentWord = CurrentWord & Mid(Input1,c,1)
        Else
            words=words+1
            **dictionary(n)=CurrentWord**
        End If

        richTextBox3.Text=("words is " & words & "current words is " & CurrentWord)
    Next        

End Sub

这是我目前所知道的,主要问题似乎与行 dictionary(n)=CurrentWord 有关。如果有人有任何想法/现有计划可以实现相同的目标,我将非常感激。谢谢!

此外,如果有帮助,它会出现以下错误消息 System.IndexOutOfRangeException: 索引超出数组范围。

可以简化代码,使用String.Split方法将文本拆分成单词数组,数组的Length属性统计单词数。

Sub Button1Click(sender As Object, e As EventArgs)
    Dim dictionary() As String = richtextbox1.Text.Split(" "c)
    Dim words As Int64 = dictionary.Length
End Sub

[回应询问如何检查是否找到单词的评论]。

您可以使用Contains 方法查看数组是否包含给定的单词。例如:

Dim searchword As String = "someword"
If dictionary.Contains(searchword) Then
    MessageBox.Show(searchword & " was found")
End If

如果您使用的 .Net Framework 版本早于 3.5 版,您可以像这样使用 IndexOf 方法(至少 Framework 2.0 及更高版本):

Dim searchword As String = "someword"
If Array.IndexOf(dictionary, searchword) >= 0 Then
    MessageBox.Show(searchword & " was found")
End If