FIND Amount in String

Ask a Question related to Macromedia ColdFusion, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. [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 }
    4. find in string
      How can i count the number of times a string appears within another string. Thanks a...
    5. find string between files
      Hi all, how can I search for a specific string inside a directory (between files) ? Thanks for your help
  3. #2

    Default 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>
    &lt;CFSET st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")&gt;
    <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:
    &lt;CFSET st1 = refindNoCase("([[:alpha:]]+)[
    ]+(\1)",testString,1,"TRUE")&gt;</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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default 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

  7. #6

    Default 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

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