12

我有一个字符串(例如:)"Hello there. My name is John. I work very hard. Hello there!",我正在尝试查找字符串的出现次数"hello there"。到目前为止,这是我拥有的代码:

Dim input as String = "Hello there. My name is John. I work very hard. Hello there!"
Dim phrase as String = "hello there"
Dim Occurrences As Integer = 0

If input.toLower.Contains(phrase) = True Then
    Occurrences = input.Split(phrase).Length      
    'REM: Do stuff
End If

不幸的是,这行代码似乎做的是每次看到第一个字母时拆分字符串phrase,在这种情况下,h。因此Occurrences = 2,我实际上得到的不是我希望的结果,而是一个更大的数字。我知道计算字符串中的分割数是一种可怕的方法,即使我确实得到了正确的答案,所以有人可以帮助我并提供一些帮助吗?

4

12 回答 12

21

还有一个想法:

Dim input As String = "Hello there. My name is John. I work very hard. Hello there!"
Dim phrase As String = "Hello there"
Dim Occurrences As Integer = (input.Length - input.Replace(phrase, String.Empty).Length) / phrase.Length

您只需要确保phrase.Length > 0.

于 2013-01-11T22:59:10.440 回答
15

最好的方法是:

Public Function countString(ByVal inputString As String, ByVal stringToBeSearchedInsideTheInputString as String) As Integer
    Return System.Text.RegularExpressions.Regex.Split(inputString, stringToBeSearchedInsideTheInputString).Length -1

End Function
于 2014-03-23T16:04:01.150 回答
5
str="Thisissumlivinginsumgjhvgsum in the sum bcoz sum ot ih sum"
b= LCase(str)
array1=Split(b,"sum")
l=Ubound(array1)
msgbox l

输出给你没有。一个字符串在另一个字符串中出现的次数。

于 2015-10-18T11:08:37.433 回答
4

您可以创建一个 Do Until 循环,一旦整数变量等于您正在检查的字符串的长度,该循环就会停止。如果短语存在,则增加出现次数并将短语的长度加上找到它的位置添加到游标变量中。如果找不到该短语,则您已完成搜索(没有更多结果),因此将其设置为目标字符串的长度。要不多次计算相同的出现次数,请仅检查从光标到循环中目标字符串的长度 (strCheckThisString)。

    Dim input As String = "hello there. this is a test. hello there hello there!"
    Dim phrase As String = "hello there"
    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 intPlaceOfPhrase As Integer = InStr(strCheckThisString, phrase)
        If intPlaceOfPhrase > 0 Then

            Occurrences += 1
            intCursor += (intPlaceOfPhrase + Len(phrase) - 1)

        Else

            intCursor = input.Length

        End If

    Loop
于 2013-01-11T21:54:33.197 回答
3

您只需要将 split 函数的输入更改为字符串数组,然后将StringSplitOptions.

试试这行代码:

Occurrences = input.Split({phrase}, StringSplitOptions.None).Length

我还没有检查过这个,但我认为你还必须考虑这样一个事实,即出现次数太高,因为你正在使用你的字符串进行拆分而不是实际计算它的次数字符串,所以我认为 Occurrences = Occurrences - 1

希望这可以帮助

于 2013-01-11T21:14:55.903 回答
2

您可以使用 IndexOf 创建递归函数。传递要搜索的字符串和要定位的字符串,每次递归都会增加一个 Counter 并将 StartIndex 设置为最后找到的索引 +1,直到不再找到搜索字符串。函数将需要通过引用传递的可选参数起始位置和计数器:

Function InStrCount(ByVal SourceString As String, _
                    ByVal SearchString As String, _
                    Optional ByRef StartPos As Integer = 0, _
                    Optional ByRef Count As Integer = 0) As Integer
    If SourceString.IndexOf(SearchString, StartPos) > -1 Then
        Count += 1
        InStrCount(SourceString, _
                   SearchString, _
                   SourceString.IndexOf(SearchString, StartPos) + 1, _
                   Count)
    End If
    Return Count
End Function

通过将字符串传递给搜索和字符串来定位以及可选的起始位置来调用函数:

Dim input As String = "Hello there. My name is John. I work very hard. Hello there!"
Dim phrase As String = "hello there"
Dim Occurrences As Integer

Occurrances = InStrCount(input.ToLower, phrase.ToLower)

请注意 .ToLower 的使用,它用于在比较中忽略大小写。如果您确实希望比较是针对特定情况的,请不要包含此指令。

于 2014-02-20T11:27:06.410 回答
2

另一种基于InStr(i, str, substr)函数的解决方案(从位置开始搜索substr有关 InStr() 的更多信息):stri

Function findOccurancesCount(baseString, subString)
    occurancesCount = 0
    i = 1
    Do
        foundPosition = InStr(i, baseString, subString) 'searching from i position
        If foundPosition > 0 Then                       'substring is found at foundPosition index
            occurancesCount = occurancesCount + 1       'count this occurance
            i = foundPosition + 1                       'searching from i+1 on the next cycle
        End If
    Loop While foundPosition <> 0
    findOccurancesCount = occurancesCount
End Function

一旦没有找到子字符串(InStr返回0,而不是在基本字符串中找到子字符串位置),搜索就结束并返回出现次数。

于 2016-08-24T09:00:43.943 回答
1

查看您最初的尝试,我发现这应该可以解决问题,因为“拆分”会创建一个数组。出现次数 = input.split(phrase).ubound

这是对 CaSe 敏感的,因此在您的情况下,该短语应等于“Hello there”,因为输入中没有“hello there”

于 2015-07-13T00:43:59.313 回答
1

扩展Sumit Kumar 的简单解决方案,这里是一个单行工作函数:

Public Function fnStrCnt(ByVal str As String, ByVal substr As String) As Integer
    fnStrCnt = UBound(Split(LCase(str), substr))
End Function

演示:

Sub testit()
    Dim thePhrase
    thePhrase = "Once upon a midnight dreary while a man was in a house in the usa."
    If fnStrCnt(thePhrase, " a ") > 1 Then
        MsgBox "Found " & fnStrCnt(thePhrase, " a ") & " occurrences."
    End If
End Sub 'testit()
于 2018-07-05T17:13:11.533 回答
0

不知道这个是不是比较明显?从开头开始longString检查下一个字符,直到 中的字符数phrase,如果phrase没有找到,则从第二个字符开始查找,等等。如果找到,则从当前位置开始,加上字符数,phrase并增加occurences

 Module Module1
Sub Main()

    Dim longString As String = "Hello there. My name is John. I work very hard. Hello there! Hello therehello there"

    Dim phrase As String = "hello There"


    Dim occurences As Integer = 0
    Dim n As Integer = 0

    Do Until n >= longString.Length - (phrase.Length - 1)
        If longString.ToLower.Substring(n, phrase.Length).Contains(phrase.ToLower) Then
            occurences += 1
            n = n + (phrase.Length - 1)
        End If
        n += 1
    Loop
    Console.WriteLine(occurences)


End Sub
End Module
于 2013-01-11T23:26:54.310 回答
0

我在 Vbscript 中使用了它,您也可以将其转换为 VB.net

Dim str, strToFind
str = "sdfsdf:sdsdgs::"
strToFind = ":"

MsgBox GetNoOfOccurranceOf( strToFind, str)

Function GetNoOfOccurranceOf(ByVal subStringToFind As String, ByVal strReference As String)
    Dim iTotalLength, newString, iTotalOccCount
    iTotalLength = Len(strReference)
    newString = Replace(strReference, subStringToFind, "")
    iTotalOccCount = iTotalLength - Len(newString)
    GetNoOfOccurranceOf = iTotalOccCount
End Function
于 2015-07-18T12:22:16.210 回答
0

我知道这个线程真的很旧,但我也有另一个解决方案:

Function countOccurencesOf(needle As String, s As String)
    Dim count As Integer = 0
    For i As Integer = 0 to s.Length - 1
        If s.Substring(i).Startswith(needle) Then
            count = count + 1
        End If
    Next
    Return count
End Function
于 2015-10-08T14:58:36.933 回答