Results 1 to 15 of 15

Thread: no of occurances of sring in file

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Exclamation no of occurances of sring in file

    I have a text file. I need a macro which will take input string from user and prints no of occurance of that string in a line.

    Exanple: hari bnsnnndn hari nbns, hari xnndd dd ;
    hari bnsnnndn hari nbns, hari xnndd dd ;
    hari bnsnnndn hari nbns, xnndd dd ;


    input string :hari
    no of occurance: 3
    3
    2

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: no of occurances of sring in file

    Splitting the string and finding the UBound of the resulting array is probably the easiest to code:
    VB Code:
    1. Private Function NumStrings(sSource As String, sMatch As String) As Long
    2.  
    3.     Dim sTemp() As String
    4.    
    5.     sTemp = Split(sSource, sMatch)
    6.     NumStrings = UBound(sTemp)
    7.  
    8. End Function

  3. #3
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: no of occurances of sring in file

    @Comintern: You probably misunderstood OP. He wants to get the number of occurences of a particular string in a line.

    Here is a function that will return the number of occurences of a particular string in a line.
    VB Code:
    1. 'Function : checks the number of occurences of sToSearch in sLine
    2. 'returns the number of occurences
    3. Public Function checkOccurences(ByVal sLine As String, ByVal sToSearch As String) As Long
    4.    
    5.     Dim searchPoistion As Long
    6.     searchPoistion = 1
    7.    
    8.     Dim numOfOccurences As Long
    9.     numOfOccurences = 0
    10.     'get the position of the sToSearch in sLine
    11.     searchPoistion = InStr(searchPoistion, sLine, sToSearch)
    12.     'if position <> 0 then first occurence found
    13.     Do While searchPoistion > 0
    14.         'increment +1
    15.         numOfOccurences = numOfOccurences + 1
    16.         'add the position of sToSearch with length of sToSearch
    17.         'so that it can find the next occurence
    18.         searchPoistion = searchPoistion + Len(sToSearch)
    19.         searchPoistion = InStr(searchPoistion, sLine, sToSearch)
    20.     Loop        'loop till we don't have any more occurences
    21.     'return the number of occurences.
    22.     checkOccurences = numOfOccurences
    23. End Function
    You can call this function like this
    VB Code:
    1. MsgBox checkOccurences("hari bnsnnndn hari nbns, hari xnndd dd ;", "hari")
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: no of occurances of sring in file

    Quote Originally Posted by Shuja Ali
    @Comintern: You probably misunderstood OP. He wants to get the number of occurences of a particular string in a line.
    Try the code I posted--it does exactly the same thing. If you use split on the line using the substring as a delimiter:

    Before split:
    "hari bnsnnndn hari nbns, hari xnndd dd ;"

    After split with "hari" delimiter:
    element 0 = ""
    element 1 = " bnsnnndn "
    element 2 = " nbns, "
    element 3 = " xnndd dd ;"

    The UBound will always equal the number of occurrences.

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: no of occurances of sring in file

    Quote Originally Posted by Comintern
    Try the code I posted--it does exactly the same thing. If you use split on the line using the substring as a delimiter:

    Before split:
    "hari bnsnnndn hari nbns, hari xnndd dd ;"

    After split with "hari" delimiter:
    element 0 = ""
    element 1 = " bnsnnndn "
    element 2 = " nbns, "
    element 3 = " xnndd dd ;"

    The UBound will always equal the number of occurrences.
    I didn't see that. I just saw split in there.

    One more reason why one shouldn't jump to conclusions.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: no of occurances of sring in file

    You don't even need the variable in yours, Comintern:
    VB Code:
    1. Private Function NumStrings(sSource As String, sMatch As String) As Long
    2.     NumStrings = UBound(Split(sSource, sMatch))
    3. End Function

    there's also another way of doing it on one line:
    VB Code:
    1. Private Function NumStrings(ByVal ssource As String, ByVal sMatch As String) As Long
    2.     NumStrings = (Len(ssource) - Len(Replace(ssource, sMatch, vbNullString))) / Len(sMatch)
    3. End Function

    All of the codes suggested here will not count words, just case you that was actually what you were after, for example:

    "hari bnsnharinndn hari nbns, hari xnndd dd ;" will give 4 instances of the string 'hari', but there are only three instances of the word 'hari'

  7. #7
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: no of occurances of sring in file

    Quote Originally Posted by bushmobile
    You don't even need the variable in yours, Comintern
    Yeah, I know--I just do it from force of habit. I seem to remember reading somewhere that UBound leaks memory when it is passed the result of the function instead of an explicit array. Something about how the array gets garbage collected--I want to say that a temp array gets built for the call to UBound, but never gets de-allocated when it should normally lose scope. Ahhh yes, I found the link.

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: no of occurances of sring in file

    I didn't know that stupid bugs

    If you're interested, I did a speed test for the split (non-memory leaking version) and the replace methods.

    Short string was just 39 characters, with the search string present 3 times. Long string was 50 short strings concatenated together. 100000 repeats of each function:

    Code:
    Len & Replace - Short String
    ------------------
     0.319812154896782 
     0.318800853181061 
     0.321015659811512 
    
    Split - Short String
    ------------------
     0.471079704264089 
     0.470355869251539 
     0.469071907183734 
    
    Len & Replace - Long String
    ------------------
     7.04909339036107 
     6.93816476675108 
     6.81726029425528 
    
    Split - Long String
    ------------------
     11.4743161491195 
     11.500835720741 
     11.6098705282375

  9. #9
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: no of occurances of sring in file

    Quote Originally Posted by bushmobile
    I didn't know that stupid bugs

    If you're interested, I did a speed test for the split and the replace methods.

    Short string was just 39 characters, with the search string present 3 times. Long string was 50 short strings concatenated together. 100000 repeats of each function:
    [SNIP]
    Not surprised at all. Your method doesn't have to build an array.

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: no of occurances of sring in file

    I suppose, but i've never found Replace to be particularly speedy. I was expecting the split to be faster. Oh well

  11. #11
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: no of occurances of sring in file

    I got the impression from the first post that he wanted it printed out for each line of the (multiline) input

    e.g.

    line 1 has 3 occurences
    line 2 has 3 occurences etc

    in which case you would have to split by the vbcrlf character, then loop through and split by the word you're looking for

    VB Code:
    1. dim lines() as string, i as integer
    2. input = somestring
    3. lines = split(somestring, vbcrlf)
    4. for i = 0 to ubound(lines)
    5. debug.print("Line " & i + 1 & " occurences: " & ubound(split(lines(i), "hari")) - 1)
    6. next

    Please forgive any syntax / coding errors, it's been a while :s

  12. #12
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: no of occurances of sring in file

    Sorry chaps. The number of instances of a string with a string (so get the file lines individually inside a string, first) has been well hammered here I think Penegate, and myself got as much mileage out of this as is humanly possible
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Re: no of occurances of sring in file

    Thanks for ur mail. Exactly I wanted it only i.e the macro should print out for each line of the (multiline) output.

    Actually I have a text file with some 100 lines , with each line is delimited by ';'. I need a macro that will print out for each line , no of occurances of a particular string .


    Answer should look like
    example: line 1 has 3 occurences
    line 2 has 3 occurences etc
    .
    .THanks & regards





    Quote Originally Posted by da_silvy
    I got the impression from the first post that he wanted it printed out for each line of the (multiline) input

    e.g.

    line 1 has 3 occurences
    line 2 has 3 occurences etc

    in which case you would have to split by the vbcrlf character, then loop through and split by the word you're looking for

    VB Code:
    1. dim lines() as string, i as integer
    2. input = somestring
    3. lines = split(somestring, vbcrlf)
    4. for i = 0 to ubound(lines)
    5. debug.print("Line " & i + 1 & " occurences: " & ubound(split(lines(i), "hari")) - 1)
    6. next

    Please forgive any syntax / coding errors, it's been a while :s

  14. #14
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: no of occurances of sring in file

    then replace vbcrlf with ";" in the code above

    i'd also be inclined to do something like
    lines = split(replace(somestring, vbcrlf, ""), ";")

    so you get rid of all the new line characters then you split based on the ";"

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: no of occurances of sring in file

    Quote Originally Posted by Comintern
    Ahhh yes, I found the link.
    Typical Microshaft. "We know we sold you a broken product. This is how it's broken. That's too bad. Fix it? You're funny."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width