Ask a Question related to Macromedia ColdFusion, Design and Development.
-
Vbprog40 #1
FIND Amount in String
I have this:
<cfset TempVar = "This is a test string to test the code">
I need to find the amount of times the word "test" appears in the above string. How can i do this?
Thank you!
Vbprog40 Guest
-
How to find second occurence of a string?
Hi i am using the find function which will search the first occurence of a string. but how we can find the second or third ...occurence of the... -
Find and cut string
Hi, I'm new to PHP, so please bear with me! =) Is there a nice little function to find a string between to html-tags? I.e. let's say I have a... -
[PHP] find string
Isn't there an in_array function you can use? If (in_array($action, array(a1,a2,a3,a4)) { // do something } else { // do something else } -
find in string
How can i count the number of times a string appears within another string. Thanks a... -
find string between files
Hi all, how can I search for a specific string inside a directory (between files) ? Thanks for your help -
The ScareCrow #2
Re: FIND Amount in String
use refind.
I have attached the help doc from studio (just copy, paste and run), the last
paragraph is what you need to do.
Ken
<!--- This example shows the use of refindNoCase --->
<html>
<head>
<title>
refindNoCase Example
</title>
</head>
<body>
<H3>refindNoCase Example</H3>
<P>This example demonstrates the use of the refindNoCase function with
and without the <i>returnsubexpressions</i> parameter set to True.</P>
<P>If you do not use the <i>returnsubexpressions</i> parameter,
refindNoCase returns the position of the first occurrence of a
regular expression in a string starting from the specified
position. Returns 0 if no occurrences are found.
</P>
<P>refindNoCase("a+c+", "abcaaccdd"):
<cfoutput>#refindNoCase("a+c+", "abcaaccdd")#</cfoutput></P>
<P>refindNoCase("a+c*", "abcaaccdd"):
<cfoutput>#refindNoCase("a+c*", "abcaaccdd")#</cfoutput></P>
<P>refindNoCase("[[:alpha:]]+", "abcaacCDD"):
<cfoutput>#refindNoCase("[[:alpha:]]+", "abcaacCDD")#</cfoutput></P>
<P>refindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u = 5"):
<cfoutput>#refindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u =
5")#</cfoutput>
</P>
<!--- Set startPos to one; returnMatchedSubexpressions = TRUE --->
<hr size = "2" color = "#0000A0">
<P>If you do use the <i>returnssubexpression</i> parameter,
refindNoCase returns the position and length of the first
occurrence of a regular expression in a string starting from
the specified position. The position and length variables are
stored in a structure. To access the position and
length information, you must use the keys <i>pos</i> and
<i>len</i>, respectively.</P>
<cfset teststring = "The cat in the hat hat came back!">
<P>The string in which the function is to search is:
<cfoutput><b>#teststring#</b></cfoutput>.</P>
<P>The first call to refindNoCase to search this string is:
<b>refindNoCase("[[:alpha:]]+",testString,1,"TRUE")</b></P>
<P>This function returns a structure that contains two arrays:
pos and len.</P>
<P>To create this structure you can use a CFSET statement,
for example:</P>
<CFSET st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")>
<cfset st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")>
<P>
<cfoutput>
The number of elements in each array: #ArrayLen(st.pos)#.
</cfoutput>
</P>
<P><b>The number of elements in the pos and len arrays will always be
one if you do not use parentheses to denote subexpressions in
the regular expression.</b></P>
<P>The value of st.pos[1] is: <cfoutput>#st.pos[1]#.</cfoutput></P>
<P>The value of st.len[1] is: <cfoutput>#st.len[1]#.</cfoutput></P>
<P>
<cfoutput>
Substring is <b>[#Mid(testString,st.pos[1],st.len[1])#]</B>
</cfoutput>
</P>
<hr size = "2" color = "#0000A0">
<P>However, if you use parentheses to denote subexpressions in the
regular expression, you will find that the first element
contains the position and length of the first instance of the
whole expression. The position and length of the first instance
of each subexpression within will be included in additional
array elements.</P>
<P>For example:
<CFSET st1 = refindNoCase("([[:alpha:]]+)[
]+(\1)",testString,1,"TRUE")></P>
<cfset st1 = refindNoCase("([[:alpha:]]+)[ ]+(\1)",testString,1,"TRUE")>
<P>The number of elements in each array is
<cfoutput>#ArrayLen(st1.pos)#</cfoutput>.</P>
<P>First whole expression match; position is
<cfoutput>
#st1.pos[1]#; length is #st1.len[1]#;
whole expression match is
<B>[#Mid(testString,st1.pos[1],st1.len[1])#]</B>
</cfoutput></P>
<P>Subsequent elements of the arrays provide the position and length of
the first instance of each parenthesized subexpression therein.</P>
<CFLOOP index = "i" from = "2" to = "#ArrayLen(st1.pos)#">
<P><cfoutput>Position is #st1.pos[i]#; Length is #st1.len[i]#;
Substring is <B>[#Mid(testString,st1.pos[i],st1.len[i])#]</B>
</cfoutput></P>
</CFLOOP><BR>
</body>
</html>
The ScareCrow Guest
-
Vbprog40 #3
Re: FIND Amount in String
Whats isa this: <cfset st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")>
How do i get it what to look for.. with the above code it along looks for the
first repeater... not all of them.
So if you have: <cfset teststring = "The two two cat in the back! four four">
Vbprog40 Guest
-
The ScareCrow #4
Re: FIND Amount in String
It's a regular expression.
I'm crap at these so I can't help there.
Ken
The ScareCrow Guest
-
BerkTheTurk #5
Re: FIND Amount in String
You might try to compare each word in your sentence to the word defined by you
to see how many times it repeated. I attached a piece code that might be use
Berk
<cfloop index="i" from="1" to"15">
<cfset word = GetToken(#sentence#, i, ' ')>
<cfif word EQ user_word>
<cfset count = count +1>
</cfif>
</cfloop>
BerkTheTurk Guest
-
OldCFer #6
Re: FIND Amount in String
Another way:
<cfset TempVar = "This is a test string to test the code">
<cfset x = 1>
<cfset cntr = 0>
<cfloop condition="#FindNoCase('test',tempvar,x)#">
<cfset cntr = cntr + 1>
<cfset x = FindNoCase('test',tempvar,x) + 1>
</cfloop>
<cfoutput>"test" appears #cntr# times</cfoutput>
OldCFer Guest



Reply With Quote

