469,362 Members | 2,498 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

counting number of results in a string search

Hi,

I have another string handling question for the group, since you have all
been so helpful in the past. Thank you.

Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the
substring appears in the mainstring

e.g.

mainstring = "The man walked through the park, the man was
happy"

substring = "man"

Count = 2 (Because 'man' appears twice)

Now, to me this sounds like a very simple thing to do, (It is, right?) but I
have been working on it for 3 hours now and it's starting to make my brain
hurt.

The problem I am having (Take the main string I mentioned earlier, searching
for "Man") is moving the searcher to the right position. The second "man"
in my mainstring is at position 38. If the searcher gets to position 10 it
finds it at position 38 (so count = 2), if it moves to position 15, it finds
it at position 3 (count = 3), and as it continues until it gets past the
position it counts it every time it sees it.

I've tried coding so that every time a position is duplicated, count = count
+ 1 - 1. It works if the position occurs twice, but anymore than that
counts it! I think somehow I need to store the previous position count in a
variable, then check it with the current one and change the count number
accordingly. But i can't seem to get there!

I think my code is nearly there, it just needs a little something extra.
Can anyone help please?

Here is my code (I popped lots of msgboxes in there to try and help me see
where I was going wrong - to no avail!):

Dim mainstring As String 'Declare Variables

Dim searchstring As String

Dim searcher As Integer

Dim count As Integer

Dim Startpoint As Integer

mainstring = " " & txtMain.Text 'Initialise variables

searchstring = txtSearch.Text

Do

Startpoint = Startpoint + Len(searchstring) + 1
'Calculate place to start searching mainstring

MsgBox ("Startpoint = " & Startpoint)

searcher = InStr(Startpoint, mainstring, searchstring) 'Search
mainstring for searchstring, starting from calculated start
point.

MsgBox searcher

If (searcher > 0) Then 'If position of searchstring in
mainstring is > 0 then

count = count + 1 'add 1 to counter

MsgBox ("+ 1 to count")

ElseIf searcher = searcher Then 'If position or searchstring is
found twice, then

count = count + 1

count = count - 1 'Minus 1 from counter

MsgBox ("-1 from count")

End If

MsgBox ("Count = " & count)

'Display position of found word

Loop Until searcher = 0 'Loop until there are no more words

End Sub

Thanks!

Cassandra

Jul 17 '05 #1
5 6478
Not very often that I feel qualified to answer a question here, but.....

Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the
substring appears in the mainstring

mainstring = "The man walked through the park, the man was
happy"

substring = "man"

Count = 2 (Because 'man' appears twice)


You can use the Instr function to do this but I found Mid to be easier:

--------------------------------
mainstring = "The man walked through the park, the man was happy "
searchstring = "man"

For x = 1 To Len(mainstring)
If UCase(searchstring) = UCase(Mid(mainstring, x, Len(searchstring))) Then
matches = matches + 1
Next x

lblNumberOfMatches.Caption = matches
---------------------------------

You can take out the Ucase function if you only want a match when the case
of all characters is the same.
eg. "Man" <> "man"

Regards,
Tom

Jul 17 '05 #2
Thank you very much, it works perfectly, I feel a real idiot for spending so
long and writing such a huge code to try and do something so easy!

I will analyse your code now and pick it to pieces to make sure i understand
exactly how to do it!

Thanks

Cassandra

"Kiteman (Canada)" <NO************@shaw.ca> wrote in message
news:aJNwb.495244$pl3.140416@pd7tw3no...
Not very often that I feel qualified to answer a question here, but.....

Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the
substring appears in the mainstring

mainstring = "The man walked through the park, the man was
happy"

substring = "man"

Count = 2 (Because 'man' appears twice)
You can use the Instr function to do this but I found Mid to be easier:

--------------------------------
mainstring = "The man walked through the park, the man was happy "
searchstring = "man"

For x = 1 To Len(mainstring)
If UCase(searchstring) = UCase(Mid(mainstring, x, Len(searchstring)))

Then matches = matches + 1
Next x

lblNumberOfMatches.Caption = matches
---------------------------------

You can take out the Ucase function if you only want a match when the case
of all characters is the same.
eg. "Man" <> "man"

Regards,
Tom

Jul 17 '05 #3
.... or alternatively ...

Private Sub Command1_Click()

Dim sSearch As String
Dim sOrig As String
Dim pos As Long
Dim lastfound As Long
Dim total As Long

sOrig = "The man walked through the manly park, the man was happy"
sSearch = "man"

pos = InStr(1, sOrig, sSearch, vbTextCompare)

If pos > 0 Then

total = 1
lastfound = pos

Do While pos <> 0

pos = InStr(lastfound + 1, sOrig, sSearch, vbTextCompare)

If pos > 0 Then
lastfound = pos
total = total + 1
End If

Loop

End If

Print total

End Sub

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Kiteman (Canada)" <NO************@shaw.ca> wrote in message
news:aJNwb.495244$pl3.140416@pd7tw3no...
: Not very often that I feel qualified to answer a question here, but.....
:
: >
: > Basically, I want to do something really simple:
: >
: > Search a main string for a substring, then count how many times the
: > substring appears in the mainstring
: >
: > mainstring = "The man walked through the park, the man was
: > happy"
: >
: > substring = "man"
: >
: > Count = 2 (Because 'man' appears twice)
:
: You can use the Instr function to do this but I found Mid to be easier:
:
: --------------------------------
: mainstring = "The man walked through the park, the man was happy "
: searchstring = "man"
:
: For x = 1 To Len(mainstring)
: If UCase(searchstring) = UCase(Mid(mainstring, x, Len(searchstring)))
Then
: matches = matches + 1
: Next x
:
: lblNumberOfMatches.Caption = matches
: ---------------------------------
:
: You can take out the Ucase function if you only want a match when the case
: of all characters is the same.
: eg. "Man" <> "man"
:
: Regards,
: Tom
:
:
:
Jul 17 '05 #4
On Tue, 25 Nov 2003 18:38:53 +0000 (UTC), "cassandra.flowers"
<ca***************@btopenworld.com> wrote:
Hi,

I have another string handling question for the group, since you have all
been so helpful in the past. Thank you.

Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the
substring appears in the mainstring


Randy pointed out a nasty problem - easily solved
There is also the matter of ,?.;: (punctuation)

If you are working on pure text
- then it would be wise to clean it up first
Jul 17 '05 #5
> I have another string handling question for the group, since you have all
been so helpful in the past. Thank you.

Basically, I want to do something really simple:

Search a main string for a substring, then count how many times the
substring appears in the mainstring

e.g.

mainstring = "The man walked through the park, the man was
happy"

substring = "man"

Count = 2 (Because 'man' appears twice)


Here's one of my one-liner alternatives.

MainString = "The man walked through the park, the man was happy"
SearchWord = "man"
Count = UBound(Split(MainString, SearchWord, , vbTextCompare))

which will count how many times the word man appears in any of these forms
man, Man, MAN, mAn, etc. Use this

Count = UBound(Split(MainString, SearchWord))

if you want an exact match case-wise.

Be aware, however, that either of these will also count embedded words such
as the "man" found in the word "command". This problem applies to all of the
posted solutions; not just those above.

Rick
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Sam Lowry | last post: by
3 posts views Thread by Megan | last post: by
7 posts views Thread by sathyashrayan | last post: by
16 posts views Thread by Leon | last post: by
44 posts views Thread by Michael McGarry | last post: by
6 posts views Thread by androoo | last post: by
27 posts views Thread by Simon Biber | last post: by
3 posts views Thread by nitric | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.