Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find number of <tag> in sentence

Status
Not open for further replies.

Bramvg

IS-IT--Management
Jan 16, 2001
135
BE
Hi,

I know there must be a very easy sollution to this problem, but I just cannot find it.... I need some coffee I guess ;-)

Here is the problem:

I would like to count how many times the word '<tag>' appears in a sentence.

E.g.: Yesterday I <tag> to the <tag> to buy some <tag>.

The result should be: 3

Many thanks guys/and or girls.
Bram
 
Bramvg,

This might work (I haven't tested)
<CFSet LX=1>
<CFLOOP Condition=&quot;#GetToken(MYStringtoSearch,LX,'<TAG>')#&quot; is not &quot;&quot;>
<CFSET LX = #LX#+1>
</CFLOOP>

<CFOUTPUT>#LX#</CFOUTPUT>

That might be your number.
The idea is that GetToken is going to use <Tag> as a delimiter and loop through until it can't find another delimiter which might put your actual number one up or one down from what this returns.

Give it a try and If I'm wrong, I haven't had coffee yet either.

Have fun
 
Hi tlhawkins,

I tried it, but I get errors on the quote signs.

This is what I tried:

<CFSet LX=1>
<CFLOOP Condition=&quot;#GetToken(&quot;Bram <tag> van <tag> grimbergen <tag>&quot;,LX,'<TAG>')#&quot; not &quot;&quot; >
<CFSET LX = #LX#+1>
</CFLOOP>

I get an error like:
Invalid token found on line 14 at position 88. ColdFusion was looking at the following text:

&quot;


When I put the code beneth outside the CFLOOP it works fine:
#GetToken(&quot;Bram <tag> van <tag> grimbergen <tag>&quot;,LX,'<TAG>')#

Hope you can help me out.
Bram
 
Hey sorry about not checking that code. I just got back in and looked at it. What a wreck.

This Acually works:

<CFSet LX=0>
<CFSET OFFset=1>
<CFLOOP Condition=&quot;OFFset is not 5 &quot; >
<CFSET LX = #LX#+1>
<CFSET OFFset = #FindNoCase(&quot;<tag>&quot;,&quot;<tag>Bram <tag> van <tag> grimb<tag>ergen <tag>&quot;,OFFset)# + 5>
</CFLOOP>

<CFOUTPUT>#LX#</CFOUTPUT>

I didn't know if case was important or not. If it is Use &quot;Find&quot; instead of &quot;FindnoCase&quot;.

GetToken didn't like the 5 character delimiter. Seemed to be useing any of those characters as a delimter. Anyway, the &quot; problem was just bad typing on my part. But this way actually works alot better. I added and removed <tag>s to check it. Seems fine!

Have fun...



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top