« iPods Integrate With BMWs | Main | Free ColdFusion Applications »
June 22, 2004
Know Your List Functions
If you use ColdFusion list functions, make sure you know the difference between listContains and listFind. Using listContains where you should be using listFind might appear to work at first, but can introduce hard-to-find bugs in your applications down the road.
listContains returns the index of the first item in the list which contains a substring of the string you are searching for. For instance, consider the following code:
<cfset myList = "abc,def,ghi"/>
<cfoutput>#listContains(myList, "e")#</cfoutput>
The substring "e" is contained in the second item in the list, so listContains returns 2 rather than 0. Now consider the code below which uses listFind:
<cfset myList = "abc,def,ghi"/>
<cfoutput>#listFind(myList, "e")#</cfoutput>
listFind looks for an exact match rather than just a substring, so 0 is returned since there is no item in the list which matches "e" exactly. (The search is case-sensitive -- for a case-insensitive search, use listFindNoCase.)
Most of the time, you are probably going to want to use listFind. Either way, just make sure you are aware of the difference.
Posted by cantrell at June 22, 2004 10:09 AM | References