How Many Times?

A

Arpan

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
 
M

McKirahan

Arpan said:
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
 
M

McKirahan

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
 
E

Evertjan.

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>
 
E

Evertjan.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,834
Members
47,382
Latest member
MichaleStr

Latest Threads

Top