CFLib.org – Common Function Library Project

findList(valueList, stringtocompare[, start][, delim])

Last updated September 15, 2008

author

Charlie Arehart

Version: 0 | Requires: CF6 | Library: StrLib

Description:
While ListFind and ListContains look for a string (or subset) within a list, FindList does the opposite, finding if any of several items in a list occur within a string. It returns the location of the first one found. This is great for testing if any of several items appear within a given string.

Return Values:
The position of first found list element in string; or 0, if no list elements are in string.

Example:

<cfset testvals="badurl1.com,www.badurl2.org,http://www.badurl3.net">

<cfset teststring="I have www.badurl2.org in here.">
<cfif findlist(testvals,teststring)>
    Found element from bad list.<br>
</cfif>

<cfset teststring="I have a badurl4.com in here.">
<cfif not findlist(testvals,teststring)>
    Did not find any elements from bad list.<br>
</cfif>

<cfset teststring="I have a www.badurl2.org in here.">
<cfset findloc = findlist(testvals,teststring)>
<cfif findloc>
    Found element from bad list at position <cfoutput>#findloc# of "#teststring#"</cfoutput>.<br>
</cfif>

<cfset teststring="I have a www.badurl2.org in here.">
<cfset start=11>
<cfset findloc = findlist(testvals,teststring,start)>
<cfif findloc>
    Found element from bad list at position <cfoutput>#findloc# of "#teststring#"</cfoutput>.<br>
<cfelse>
    Did not find element from bad list in <cfoutput>"#teststring#" starting at position #start#</cfoutput>.<br>
</cfif>

<cfset teststring="I have a www.badurl2.org in here.">
<cfset start=10>
<cfset findloc = findlist(testvals,teststring,start)>
<cfif findloc>
    Found element from bad list at position <cfoutput>#findloc# of "#teststring#"</cfoutput>.<br>
<cfelse>
    Did not find element from bad list in <cfoutput>"#teststring#" starting at position #start#</cfoutput>.<br>
</cfif>

Parameters:

Name Description Required
valueList List of values to check for. Yes
stringtocompare String to be searched. Yes
start Optional starting position. Defaults to 1. No
delim List delimiter. Defaults to a comma. No

Full UDF Source:

<!---
 Finds within a given string the location of the first occurrence of any element in a list.
 
 @param valueList      List of values to check for. (Required)
 @param stringtocompare      String to be searched. (Required)
 @param start      Optional starting position. Defaults to 1.  (Optional)
 @param delim      List delimiter. Defaults to a comma. (Optional)
 @return The position of first found list element in string; or 0, if no list elements are in string. 
 @author Charlie Arehart (charlie@carehart.org) 
 @version 0, September 15, 2008 
--->
<cffunction name="findList" returnType="numeric" output="false">
    <cfargument name="valuelist" required="Yes" type="string">
    <cfargument name="stringtocompare" required="Yes" type="string">
    <cfargument name="start" required="No" type="numeric" default="0">
    <cfargument name="delim" required="no" type="string" default=",">
    <cfset var test=arrayNew(1)>
    <cfset var x = "">


    <cfloop list="#arguments.valuelist#" index="x" delimiters="#arguments.delim#">
        <cfset ArrayAppend(test,findnocase(x,arguments.stringtocompare, arguments.start)) />
    </cfloop>

    <cfreturn arrayMin(test) />
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson