1

这里是初学者,请多多包涵,如有错误,我提前道歉。这是一些家庭作业,我遇到了一些麻烦。

总体目标:使用循环语句输出字符串中特定数量的字符。例如,用户想在“鸡为什么过马路?”中找到多少个“我”,答案应该是 2。

1) 表单/gui 有 1 个多行文本框和 1 个标题为“搜索”的按钮

2) 用户在文本框中输入/复制/粘贴文本点击“搜索”按钮

3)搜索按钮打开一个输入框,用户将在其中输入他们想要在文本框中搜索的字符,然后按“确定”

4)(我真的需要帮助)使用循环语句,程序搜索并计算文本输入到Inputbox的次数,出现在MultiLine Textbox内的文本中,然后显示字符出现的次数在“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"))
4

5 回答 5

0

概念: Increment直到count包含input特定search字符串。如果它containssearch字符串,则replace第一次出现string.empty(In 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)
于 2013-04-02T07:37:59.977 回答
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)
于 2013-04-02T07:57:43.723 回答
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 因为你的行中有两个 i 。

希望这可以帮助!

编辑:

这仅在您搜索单个字符而不是单词时才有效

于 2013-04-02T08:15:47.843 回答
0

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

于 2013-04-02T06:31:36.993 回答
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

您不仅会拥有出现次数,而且还会拥有文件中的索引位置,并且可以轻松地在字典中分组。

于 2013-04-02T09:15:21.267 回答