【发布时间】:2016-09-29 17:18:38
【问题描述】:
所以我正在尝试使用 Visual Basic 在 Visual Studio 中创建一个 Windows 窗体应用程序,该应用程序有一个文本框供您输入句子,另一个文本框供您输入该句子中的单词,然后是一个按钮点击句子中输入的单词的位置被输出到标签上。目前,代码计算一个单词的出现次数,但我想对其进行调整,以便计算单词出现的位置(例如,在“Hello hello hello”中输入“hello”并输出 1 2 3。
当前代码低于任何帮助将不胜感激。
Public Class Form1
'first attempt not working
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Dim input As String = textBox1.Text
Dim word As String = textBox2.Text
Dim occurrences As Integer = 0
Dim intCursor As Integer = 0
Do Until intCursor >= input.Length
Dim strCheckThisString As String = Mid(LCase(input), intCursor + 1, (Len(input) - intCursor))
Dim intPlaceOfWord As Integer = In-Str(strCheckThisString, word)
If intPlaceOfWord > 0 Then
occurrences += 1
intCursor += (intPlaceOfWord + Len(word) - 1)
Else
intCursor = input.Length
End If
Loop
Positions.text = occurrences
End Sub
End Class
【问题讨论】:
-
查看String.IndexOf,特别是这个重载msdn.microsoft.com/en-us/library/7cct0x33(v=vs.110).aspx
标签: vb.net string substring counting basic