|
-
Mar 21st, 2006, 01:54 AM
#1
Thread Starter
New Member
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
-
Mar 21st, 2006, 10:56 AM
#2
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:
Private Function NumStrings(sSource As String, sMatch As String) As Long
Dim sTemp() As String
sTemp = Split(sSource, sMatch)
NumStrings = UBound(sTemp)
End Function
-
Mar 21st, 2006, 11:09 AM
#3
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:
'Function : checks the number of occurences of sToSearch in sLine
'returns the number of occurences
Public Function checkOccurences(ByVal sLine As String, ByVal sToSearch As String) As Long
Dim searchPoistion As Long
searchPoistion = 1
Dim numOfOccurences As Long
numOfOccurences = 0
'get the position of the sToSearch in sLine
searchPoistion = InStr(searchPoistion, sLine, sToSearch)
'if position <> 0 then first occurence found
Do While searchPoistion > 0
'increment +1
numOfOccurences = numOfOccurences + 1
'add the position of sToSearch with length of sToSearch
'so that it can find the next occurence
searchPoistion = searchPoistion + Len(sToSearch)
searchPoistion = InStr(searchPoistion, sLine, sToSearch)
Loop 'loop till we don't have any more occurences
'return the number of occurences.
checkOccurences = numOfOccurences
End Function
You can call this function like this
VB Code:
MsgBox checkOccurences("hari bnsnnndn hari nbns, hari xnndd dd ;", "hari")
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Mar 21st, 2006, 11:18 AM
#4
Re: no of occurances of sring in file
 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.
-
Mar 21st, 2006, 11:26 AM
#5
Re: no of occurances of sring in file
 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
-
Mar 21st, 2006, 02:09 PM
#6
Re: no of occurances of sring in file
You don't even need the variable in yours, Comintern:
VB Code:
Private Function NumStrings(sSource As String, sMatch As String) As Long
NumStrings = UBound(Split(sSource, sMatch))
End Function
there's also another way of doing it on one line:
VB Code:
Private Function NumStrings(ByVal ssource As String, ByVal sMatch As String) As Long
NumStrings = (Len(ssource) - Len(Replace(ssource, sMatch, vbNullString))) / Len(sMatch)
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'
-
Mar 21st, 2006, 02:23 PM
#7
Re: no of occurances of sring in file
 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.
-
Mar 21st, 2006, 03:00 PM
#8
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
Last edited by bushmobile; Mar 21st, 2006 at 03:03 PM.
-
Mar 21st, 2006, 03:04 PM
#9
Re: no of occurances of sring in file
 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.
-
Mar 21st, 2006, 03:06 PM
#10
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
-
Mar 22nd, 2006, 03:08 AM
#11
Conquistador
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:
dim lines() as string, i as integer
input = somestring
lines = split(somestring, vbcrlf)
for i = 0 to ubound(lines)
debug.print("Line " & i + 1 & " occurences: " & ubound(split(lines(i), "hari")) - 1)
next
Please forgive any syntax / coding errors, it's been a while :s
-
Mar 22nd, 2006, 06:27 AM
#12
Frenzied Member
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
-
Mar 23rd, 2006, 01:35 AM
#13
Thread Starter
New Member
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
 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:
dim lines() as string, i as integer
input = somestring
lines = split(somestring, vbcrlf)
for i = 0 to ubound(lines)
debug.print("Line " & i + 1 & " occurences: " & ubound(split(lines(i), "hari")) - 1)
next
Please forgive any syntax / coding errors, it's been a while :s
-
Mar 23rd, 2006, 04:26 AM
#14
Conquistador
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 ";"
-
Apr 9th, 2006, 04:58 PM
#15
Re: no of occurances of sring in file
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|