473,401 Members | 2,139 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,401 software developers and data experts.

How Many Times?

Consider the following sentence:

Peter and I play together and also go to the same school and both of us
are in class V.

How do I find out how many times the string "and" has appeared in the
above sentence?

Thanks,

Arpan

Sep 17 '05 #1
4 1365
"Arpan" <ar******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Consider the following sentence:

Peter and I play together and also go to the same school and both of us
are in class V.

How do I find out how many times the string "and" has appeared in the
above sentence?

Thanks,

Arpan


Will this help? Watch for word-wrap.

Option Explicit

Const cAND = " and "
Const cLIN = "Peter and I play together and also go to the same school and
both of us are in class V."

MsgBox and1(cLIN),vbInformation,"1"
MsgBox and2(cLIN),vbInformation,"2"

Function and1(s)
Dim i, j
i = 0
Do
and1 = i
j = InStr(s,cAND)
If j = 0 Then Exit Do
s = Mid(s,j+Len(cAND)+1)
i = i + 1
Loop
End Function

Function and2(s)
Dim i, m, r, x
Set r = New RegExp
r.Pattern = cAND
r.IgnoreCase = False
r.Global = True
Set m = r.Execute(s)
For Each x in m
i = i + 1
Next
and2 = i
End Function
Sep 17 '05 #2
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:wO********************@comcast.com...
"Arpan" <ar******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Consider the following sentence:

Peter and I play together and also go to the same school and both of us
are in class V.

How do I find out how many times the string "and" has appeared in the
above sentence?

Thanks,

Arpan


A revised version:
Option Explicit

Const cP = " and "
Const cS = "Peter and I play together and also go to the same school and
both of us are in class V."

MsgBox one(cS,cP),vbInformation,"one()"
MsgBox two(cS,cP),vbInformation,"two()"

Function one(s,p)
Dim i, j
i = 0
Do
one = i
j = InStr(s,p)
If j = 0 Then Exit Do
s = Mid(s,j+Len(p)+1)
i = i + 1
Loop
End Function

Function two(s,p)
Dim i, m, r, x
i = 0
Set r = New RegExp
r.Pattern = p
r.IgnoreCase = False
r.Global = True
Set m = r.Execute(s)
For Each x in m
i = i + 1
Next
two = i
End Function
Sep 17 '05 #3
Arpan wrote on 17 sep 2005 in microsoft.public.inetserver.asp.general:
Peter and I play together and also go to the same school and both of us
are in class V.

How do I find out how many times the string "and" has appeared in the
above sentence?


<script runat='server' language='Jscript'>

s='Peter and I play together and also go to the same school'
s+='and both of us are in class V.'

alert(s)

a = s.match(/\band\b/g)

response.write(a.length)

</script>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 17 '05 #4
Evertjan. wrote on 17 sep 2005 in microsoft.public.inetserver.asp.general:

<script runat='server' language='Jscript'>

s='Peter and I play together and also go to the same school'
must add a space here:

s='Peter and I play together and also go to the same school '
s+='and both of us are in class V.'

alert(s)

a = s.match(/\band\b/g)

response.write(a.length)
without the added space the answer is 2
wth the space 3.

</script>


Jscript and vbscript are equal languages on the ASP platform.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
1
by: Anthony | last post by:
Here is a debate we have been having at work about the design of our timeclock database application. We have built an online timeclock system for companies to use to keep track of their employees...
10
by: Sunny K | last post by:
Hi guys, I have a field in my DB called EventDate as a DateTime field, therefore it holds both the date and time together like this: '2004-10-14 08:42:57.000'. I need to add together all the...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
14
by: Steve McLellan | last post by:
Hi, Sorry to repost, but this is becoming aggravating, and causing me a lot of wasted time. I've got a reasonably large mixed C++ project, and after a number of builds (but not a constant...
87
by: John Rivers | last post by:
Hello everybody, I just wondered if anybody else has noticed this? It takes around 6 seconds to start debugging a very simple ASPX page with VS.NET whereas VB6 takes under 0.5 seconds, even...
4
by: viuxrluxvbbc | last post by:
Hi im trying to write a program that will read in numbers and display them in ascending order along with a count of how many times it repeats. i got the numerical order portion done but cant figure...
4
by: kwatch | last post by:
Hi, I have a question about os.times(). os.times() returns a tuple containing user time and system time, but it is not matched to the result of 'time' command. For example, os.times() reports...
2
by: hari | last post by:
Hi all, suppose a hex value is there(say 3B), this needs to be expanded. Suppose if this 3B(0011 1011) needs to be expanded by 3 times, then each bit should be expanded by 3 times(000000111111...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.