Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
Mueller1 #1
Find multiple instances of a word inside a string
I'm trying to find multiple instances of a word inside a string. The problem
with the ColdFusion 'Find' function is that it only finds the first occurence.
I need to find all occurences. Even just the number of occurences might be
sufficient.
For example, I need to find out how many times "the" appeared in a given
string.
I checked the following ColdFusion documentation...
[url]http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functa63.htm[/url]
... but the example doesn't seem to do what I want to do. Am I missing
something?
Mueller1 Guest
-
Multiple DW IDE Instances
Not sure what topic this would fall under... We use DW to edit independant HTML files. Is it possible to launch separate instances of the DW IDE... -
Multiple Instances
We have several IIS websites set up on our server and we want one coldfusion instance per website. We have an internal IP address assigned to each... -
Please help: Prevent Multiple Instances
Hi All, How can I prevent a script from running when a previous instance of the script had already been triggered and the script is running in... -
Draging multiple instances of multiple objects
When I try this - dragging multiple instances of multiple objects onto a canvas area i have made the object instances dont stay around... For... -
Multiple instances of a control? <img> ??
Is it possible to have multiple instances of a control and still access all of them in codebehind? For example, I need to be able to change an... -
Iceborer #2
Re: Find multiple instances of a word inside a string
How about something like this using REFind?
You can always use arrayLen on the returned POS member to see how many matches
were found (if any).
Cheers
<cffunction name="findAll" type="public" output="no" returnType="struct">
<!--- Begin parameter list --->
<cfargument name="stringToSearch" type="string" required="yes">
<cfargument name="valueToFind" type="string" required="yes">
<!--- End parameter list --->
<!--- Create the return structure --->
<cfset foundItems = structNew()>
<cfset dummy = structInsert (foundItems, "LEN", arrayNew(1))>
<cfset dummy = structInsert (foundItems, "POS", arrayNew(1))>
<cfset startPos = 1>
<cfloop condition = "startPos gt 0">
<cfset tempResult = REFind (valueToFind, stringToSearch, startPos,
"true")>
<cfif tempResult.pos[1] is not 0>
<cfset dummy = arrayAppend (foundItems.LEN, tempResult.LEN)>
<cfset dummy = arrayAppend (foundItems.POS, tempResult.POS)>
<cfset startPos = tempResult.pos[1] + tempResult.len[1] + 1>
<cfelse>
<cfset startPos = 0>
</cfif>
</cfloop>
<cfreturn foundItems>
</cffunction>
<!--- Find something useless to test --->
<cfset theString="Hello I would like to say 'Hello'">
<cfset findMe = "Hello">
<cfset results = findAll (theString, findMe)>
<cfdump var="#results#">
Iceborer Guest
-
shawnwindler #3
Re: Find multiple instances of a word inside a string
Why not use Find? Just loop until Find returns 0. Increment the start value
of the Find function based on the previous Find. That kinda sounds confusing.
Something like the attached code may work. I didn't try it out, so it might
need some touch-up. - Shawn
<cfset count = -1>
<cfset start = 1>
<cfloop condition="start NEQ 0">
<cfset count += 1>
<cfset start = Find("substring", "string", start)>
</cfloop>
shawnwindler Guest
-
Unregistered #4
Find multiple instances of a word inside a string
<cfset string = "The dog walked through the neighborhood behind the house." />
<cfset count = 0 />
<cfset pos = "" />
<cfloop from="1" to="#listLen(variables.string," ")#" index="the" step="1">
<cfif listGetAt(variables.string,variables.the," ") IS "the">
<cfset count += 1 />
<cfset pos = listAppend(variables.pos,"#variables.the#",",") />
</cfif>
</cfloop>
<cfif listLen(variables.pos,",") GT 0><cfoutput>
We found the word "the" within the sentence/string #listLen(variables.pos,",")# times. At positions:
<cfloop list="#variables.pos#" index="thePos" delimiters=",">
#variables.thePos#
</cfloop></cfoutput>
</cfif>Unregistered Guest



Reply With Quote

