Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
counting number of results in a string search
Old
  (#1)
cassandra.flowers
Guest
 
Posts: n/a
Default counting number of results in a string search - 06-04-2007, 08:52 AM

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!



C***andra



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: counting number of results in a string search
Old
  (#2)
Kiteman \(Canada\)
Guest
 
Posts: n/a
Default Re: counting number of results in a string search - 06-04-2007, 08:52 AM

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



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: counting number of results in a string search
Old
  (#3)
cassandra.flowers
Guest
 
Posts: n/a
Default Re: counting number of results in a string search - 06-04-2007, 08:52 AM

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

C***andra

"Kiteman (Canada)" <NO-EMAIL REMOVED> 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
>
>
>



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: counting number of results in a string search
Old
  (#4)
Randy Birch
Guest
 
Posts: n/a
Default Re: counting number of results in a string search - 06-04-2007, 08:52 AM

.... 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-EMAIL REMOVED> 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
:
:
:


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: counting number of results in a string search
Old
  (#5)
J French
Guest
 
Posts: n/a
Default Re: counting number of results in a string search - 06-04-2007, 08:52 AM

On Tue, 25 Nov 2003 18:38:53 +0000 (UTC), "c***andra.flowers"
<c***EMAIL REMOVED> 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: counting number of results in a string search
Old
  (#6)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: counting number of results in a string search - 06-04-2007, 08:52 AM

> 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top