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?
Visual Basic.NET

Avatar of undefined
Last Comment
vbMarkO

8/22/2022 - Mon
andrux

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

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

andrux

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
appari

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
andrux

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)
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
appari

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
Luis Pérez

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
vbMarkO

ASKER
Dead on perfect ... much appreciated ... great expert
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck