Benefits of using REFind vs contains in ColdFusion

If you have a need to break apart a serialized ARGUMENT string from jQuery and JSON, say for list of questions that are getting submitted @ one time it is best to use REFind rather than contains take a look at the example below. 

The url encoded string has answers, feedback elements and urls for a single question. After decoding the string and replacing the + and & symbols that are added when doing an ajax post. (as an aside you will need to decode twice to show the special characters when rendering)

 

Once the decoding and replacing is done you will need to loop through the string and in that loop you will need to have a series of if statements that hit on your key word combos. I'm looking for answerA=,feedbackA= and urlA= so on and so fourth  I need to remove these key word combo's and add just them in their own respective lists.

So why not use contains? I'll tell you, if your if statement looks like this <cfif list contains "answer"> for example, and the list has the word answer in multiple locations and not just where you expect it. It will break at that point and will leaving you scratching your head as to why your new list count is off.

To use the REFind function REFind("your expression", "where it occurs in the loop") 

Take a look at my complete code below.

 

<cfset feedback = "">
<cfset answers = "">
<cfset urls = "">
<cfset count = 0>
<cfset newCount = 0>
<cfset thelist = "answerA%3DHypothesis%2Btesting%26answerB%3DListing%2Ball%2Bpossible%2Bdiagnoses%26answerC%3DTaking%2Byour%2Bbest%2Bguess%2Bbased%2Bupon%2Bthe%2BH%2526P%26answerD%3DListing%2Bthe%2Bmost%2Breason%2Bfor%2Bhospital%2Badmissions%2Bin%2Byour%2Bgeographic%2Barea%26feedbackA%3DCorrect!%2BEach%2Bpossible%2Bdiagnosis%2Bshould%2Bbe%2Bconsidered%2Ba%2Bhypothesis.%2BPatients'%2Bresponses%2Bto%2Bquestions%252C%2Bphysical%2Bfindings%2Band%2Bdiagnostic%2Btests%2Bare%2Bused%2Bas%2Bevidence%2Bto%2Bsupport%2Bspecific%2Bdiagnoses.%26feedbackB%3DThis%2Bis%2Ba%2Bgood%2Bstart%2Bbut%2Bnot%2Bthe%2Bbest%2Banswer.%2BTry%2Bagain.%26feedbackC%3DThis%2Bis%2Ba%2Bgood%2Bstart%2Bbut%2Bnot%2Bthe%2Bbest%2Banswer.%2BTry%2Bagain.%26feedbackD%3DNot%2Bthe%2Bbest%2Banswer%252C%2Btry%2Bagain.%26urlA%3D%26urlB%3D%26urlC%3D%26urlD%3D">
 
<cfset dynamicFields = urldecode(thelist) />
<cfset replace = Replace(#dynamicFields#,'+'," ","All") />
<cfset dynamicFields = ReReplaceNoCase(replace,'&',"^*","all" )>
 
<cflooplist="#dynamicFields#"index="j"  delimiters="^*">
<cfif  REFind("answer[A-F]=", "#j#")>
<cfset replace = ReReplace(#j#,'answer[A-F]=',"","All")/>
<cfset answers = listAppend(answers,replace,'|')>
<cfset count = count+1>
<cfelseif REFind("feedback[A-F]=","#j#")>
<cfset newCount = newCount+1>
<cfset replaceF = ReReplace(#j#,'feedback[A-F]=',"","All")/>  
 <cfset feedback = listAppend(feedback,replaceF,'|')>  
<cfelseif REFind("url[A-F]=","#j#")>
<cfset replace = ReReplace(#j#,'url[A-F]=',"","All")/>  
<cfif replace eq ''>
     <cfset replace = 'none'>
</cfif>
  <cfset urls = listAppend(urls,replace,'|')>  </cfif>
</cfloop>
<br /><br />
<strong>Answers</strong><cfdump var="#urldecode(answers)#"><br />
<strong>Feedback</strong><cfdump var="#urldecode(feedback)#"><br />
<cfdump var="#urls#"><br />
<cfdump var="#newCount#"> 
 

Answers Hypothesis testing|Listing all possible diagnoses|Taking your best guess based upon the H&P|Listing the most reason for hospital admissions in your geographic area 

Feedback Correct! Each possible diagnosis should be considered a hypothesis. Patients' responses to questions, physical findings and diagnostic tests are used as evidence to support specific diagnoses.|This is a good start but not the best answer. Try again.|This is a good start but not the best answer. Try again.|Not the best answer, try again. 

URLs none|none|none|none 

4

As  you can see  my results are as expected every answer is in the answer list using the | as a delimiter same with Feedback and URL. Using the REFind allowed me to use regular expressions to find exactly what I was looking for. Thus not adding a delimiter where I wasn't expecting.