borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (edit text file running shell batch script)Next Topic (ShellExecuteEx) New Topic New Poll Post Reply
AndreaVB Forum : API : How many times substring occurs in string?
Poster Message
hutton
Level: Protégé

Registered: 05-08-2005
Posts: 6

icon How many times substring occurs in string?

Well, I have a string that is 76 characters long and it is made up of random letters from a to e. I would like to use something like instr to tell me how many times "a" apears, "b", etc. I can see how to do a brute-force sort of thing, but I hate to miss out on some beautifully-compact method.
thanks
h

07-10-2005 at 08:43 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1617
icon Re: How many times substring occurs in string?

I believe the following would be quicker (although... the replace() command does return the number of letters replaced)


    Dim sTmp As String
    Dim i As Integer
    Dim r As Integer
    Dim ac As Integer
    Dim bc As Integer
    Dim cc As Integer
    Dim dc As Integer
    Dim ec As Integer
    
' Just to populate the variable, not used by you
'    For i = 0 To 75
'        r = CInt(Rnd() * 5 + 1)
        
'        sTmp = sTmp & Chr(r + 64)
'    Next
' End of unused code
    
    For i = 1 To Len(sTmp)
        Select Case UCase(Mid(sTmp, i, 1))
        Case "A"
            ac = ac + 1
        Case "B"
            bc = bc + 1
        Case "C"
            cc = cc + 1
        Case "D"
            dc = dc + 1
        Case "E"
            ec = ec + 1
        End Select
    Next
    
    MsgBox sTmp & vbCrLf & "A=" & ac & ", B=" & bc & ", CB=" & cc & ", D=" & dc & ", E=" & ec

' or, although could be slower a tad
    Replace sTmp, "A", "", , ac, vbTextCompare
    Replace sTmp, "B", "", , bc, vbTextCompare
    Replace sTmp, "C", "", , cc, vbTextCompare
    Replace sTmp, "D", "", , dc, vbTextCompare
    Replace sTmp, "E", "", , ec, vbTextCompare
    
    MsgBox sTmp & vbCrLf & "A=" & ac & ", B=" & bc & ", CB=" & cc & ", D=" & dc & ", E=" & ec



____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

07-10-2005 at 09:34 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : API : How many times substring occurs in string?
Previous Topic (edit text file running shell batch script)Next Topic (ShellExecuteEx) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder