473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6713
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
44
by: Michael McGarry | last post by:
Hi, Is there a standard library function that counts the number of occurences of a character in a string? Regards, Michael
6
by: androoo | last post by:
Hello all, I have a string for example : strTest = "a lineof text (60) witha number in it" I need to extract the number from between the brackets, the postion of the number in brackets is...
27
by: Simon Biber | last post by:
I was reading http://en.wikipedia.org/wiki/Poker_probability which has a good description of how to count the frequency of different types of poker hands using a mathematical approach. A sample...
3
by: nitric | last post by:
hey guys, i'm really stuck on this program. It's basically a survey and I have to ask people what drinks they like. 1-4, coffee tea oj and lemonade. i'm having trouble counting the TOTAL NUMBER...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.