Find multiple instances of a word inside a string

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Smile 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139