Link to home
Start Free TrialLog in
Avatar of vbMarkO
vbMarkO

asked on

Need to count how many times a word appears in very long string

I have a long string I need to count how many times a word appears in that string

How would I go about this?
Avatar of andrux
andrux
Flag of Mexico image

Is that a specific word or you want to count how many times all words exists in that string?

You can try using the Scripting.dictionary object to keep track of words:

And even use RegExp to get rid of punctuation marks and stuff...
Set objDict = CreateObject("Scripting.Dictionary")
Dim myStr As String = "a very very long string"

For Each strWord In Split(myStr," ")
    If Not objDict.Exists(strWord) then
        objDict.Add(strWord, 1)
    Else
        objDict.Item(strWord) += 1
    End If
Next

For Each strKey In objDict.Keys
    Msgbox(strKey & " appears " & objDict.Item(strKey) & " times")
Next

Open in new window

Avatar of vbMarkO
vbMarkO

ASKER

Its a specific word ..... it can exist in a string numerous times depending on user iput


I simply need to count how many times the word shows up in a string ....

FOr a simple example

Dim myString = "AppleblahBlahmoreblahblah AppleblahblahblahAppleBlahblah another AppleBlahblah"

So how many times does apple appear in this string ...
Now its ok if the word  is placed in an array to be counted ... just couldnt think of how to do that in this case cause its not a simple delimited string

the RegExp code would be something like this:

Dim RegEx As New Regex("[^a-z]")
and
    strWord = RegEx.Replace(strWord,"")

so, the complete code would be:
Set objDict = CreateObject("Scripting.Dictionary")
Dim RegEx As New Regex("[^a-z]")
Dim myStr As String = "a very very long string"

For Each strWord In Split(myStr," ")
    strWord = RegEx.Replace(strWord,"")
    If Not objDict.Exists(strWord) then
        objDict.Add(strWord, 1)
    Else
        objDict.Item(strWord) += 1
    End If
Next

For Each strKey In objDict.Keys
    Msgbox(strKey & " appears " & objDict.Item(strKey) & " times")
Next

Open in new window

or try this

Dim myString as string = "AppleblahBlahmoreblahblah AppleblahblahblahAppleBlahblah another AppleBlahblah"
Dim searchString as string = "apple"

dim noofOcc as integer = mystring.replace(searchstring,"").length/searchstring.length
msgbox noofOcc
Ok, then maybe something like this?

Dim RegEx As New Regex
Dim myStr As String = "AppleblahBlahmoreblahblah AppleblahblahblahAppleBlahblah "
Dim intCount As Integer = Regex.Matches(myStr, "apple").Count
Msgbox(intCount)
Avatar of vbMarkO

ASKER

Testing these now ...

aparri ... your code returned an impossible number ..... there are 4 apples it returns 12
Perhaps I did it wrong so here is what I did

 Dim myString As String = "AppleblahBlahmoreblahblah AppleblahblahblahAppleBlahblah another AppleBlahblah"
        Dim searchString As String = "Apple"

        Dim nameInt As Integer = myString.Replace(searchString, "").Length / searchString.Length
        MsgBox(nameInt)

andrux ... couldnt test yours ... first I am not sure about regex so wasnt sure what to do with the error I got
I am assuming I need to imports some sort of system reg or someting
at this line; Dim RegEx As New Regex
It syas Type 'Regex' is not defined

sorry i missed one part

 Dim searchString As String = "Apple"

        Dim nameInt As Integer = (myString.Length - myString.Replace(searchString, "").Length) / searchString.Length
        MsgBox(nameInt)
ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vbMarkO

ASKER

Roland that was perfect ....

Nailed it the first time without  any trouble ...

I am going to count this as the SOlution

I appreciate the help
Avatar of vbMarkO

ASKER

Dead on perfect ... much appreciated ... great expert