初学者,请耐心等待,我提前为任何错误道歉。 这是一些功课,我遇到了一些麻烦。
总体目标:使用循环语句输出字符串中的特定字符数。例如,用户想要在“为什么鸡过马路?”中找到多少“我”,答案应该是2。
1)表单/ gui有1个MultiLine文本框和1个标题为“Search”的按钮
2)用户在文本框中输入/复制/粘贴文本,单击“搜索”按钮
3)搜索按钮打开一个InputBox,用户将在文本框中键入要搜索的字符,然后按“确定”
4)(我真的需要帮助)使用循环语句,程序搜索并计算文本输入输入框<的次数/ strong>,出现在 MultiLine文本框内的文字中,然后,显示字符在“messagebox.show”中显示的次数
到目前为止我所拥有的一切
Private Sub Search_btn_Click(sender As System.Object, e As System.EventArgs) Handles Search_btn.Click
Dim counterInt As Integer = 0
Dim charInputStr As String
charInputStr = CStr(InputBox("Enter Search Characters", "Search"))
答案 0 :(得分:1)
我会使用String.IndexOf(string, int)
方法来获取它。简单的概念示例:
Dim input As String = "Test input string for Test and Testing the Test"
Dim search As String = "Test"
Dim count As Integer = -1
Dim index As Integer = 0
Do
index = input.IndexOf(search, index) + 1
count += 1
Loop While index > 0
由于count
循环使用, -1
初始化为do-while
- 即使输入字符串中没有出现模式,也会将其设置为0
。
答案 1 :(得分:0)
概念: Increment
count
,直到input
包含特定的search
字符串。如果contains
字符串为search
,则replace
首次出现string.empty
(在String.contains()
中,搜索从其first index
开始,即0
)
Dim input As String = "Test input string for Test and Testing the Test"
Dim search As String = "T"
Dim count As Integer = 0
While input.Contains(search) : input = New Regex(search).Replace(input, String.Empty, 1) : count += 1 : End While
MsgBox(count)
修改强>
另一种解决方案:
Dim Input As String = "Test input string for Test and Testing the Test"
Dim Search As String = "Test"
MsgBox((Input.Length - Input.Replace(Search, String.Empty).Length) / Search.Length)
答案 2 :(得分:0)
试用此代码
Dim input As String = "Test input string for Test and Testing the Test"
Dim search() As String = {"Te"}
MsgBox(input.Split(input.Split(search, StringSplitOptions.None), StringSplitOptions.RemoveEmptyEntries).Count)
答案 3 :(得分:0)
尝试此代码....未经测试但我知道我的vb:)
Function lol(ByVal YourLine As String, ByVal YourSearch As String)
Dim num As Integer = 0
Dim y = YourLine.ToCharArray
Dim z = y.Count
Dim x = 0
Do
Dim l = y(x)
If l = YourSearch Then
num = num + 1
End If
x = x + 1
Loop While x < z
Return num
End Function
它是一个使用自己的计数器的函数...对于字符串中的每个字符,它将检查该字符是否是您设置的字符(YourSearch),然后它将返回它找到的项目数。所以在你的情况下,它会返回2,因为你的行中有两个我。
希望这有帮助!
修改强>
这仅适用于搜索单个字符而不是单词
的情况答案 4 :(得分:0)
你可以尝试这样的事情:
Dim sText As String = TextBox1.Text
Dim searchChars() As Char = New Char() {"i"c, "a"c, "x"c}
Dim index, iCont As Integer
index = sText.IndexOfAny(searchChars)
While index >= 0 Then
iCont += 1
index = sText.IndexOfAny(searchChars, index + 1)
End While
Messagebox.Show("Characters found " & iCont & " times in text")
如果您想搜索单词和每个单词出现的时间,请尝试以下方法:
Dim text As String = TextBox1.Text
Dim wordsToSearch() As String = New String() {"Hello", "World", "foo"}
Dim words As New List(Of String)()
Dim findings As Dictionary(Of String, List(Of Integer))
'Dividing into words
words.AddRange(text.Split(New String() {" ", Environment.NewLine()}, StringSplitOptions.RemoveEmptyEntries))
findings = SearchWords(words, wordsToSearch)
Console.WriteLine("Number of 'foo': " & findings("foo").Count)
使用此功能:
Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
Dim dResult As New Dictionary(Of String, List(Of Integer))()
Dim i As Integer = 0
For Each s As String In wordsToSearch
dResult.Add(s, New List(Of Integer))
While i >= 0 AndAlso i < allWords.Count
i = allWords.IndexOf(s, i)
If i >= 0 Then dResult(s).Add(i)
i += 1
End While
Next
Return dResult
End Function
您不仅会有出现次数,还会有文件中的索引位置,可以轻松地在字典中分组。