CFLib.org – Common Function Library Project

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

Last updated September 15, 2008

Version: 0 | Requires: ColdFusion MX | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<!---
 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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Rick Root Rick Root added
deleteDirectory
2 day(s) ago

Troy Pullis Troy Pullis added
firstXDayOfMonth
a while ago

Henry Ho Henry Ho added
arrayMap
a while ago

Henry Ho Henry Ho added
queryGetRow
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 45 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson