How do I find a value in a list?

ColdFusion provides four functions that can help you find a value in a list. The first two are related: listFind() and listFindNoCase(). Both functions will search a list for a value. The first is case sensitive while the second will ignore case.

<cfset list = "Raymond,Jacob,Lynn,Noah,Jeanne">
<cfif listFindNoCase(list, "jacob")>
	Jacob is in the list.
</cfif>

The above code snippet will find a match on the word "jacob" even though the case does not match.

The next two related functions are listContains() and listContainsNoCase(). These functions allow for partial matches. So for example:

<cfset list = "Raymond,Jacob,Lynn,Noah,Jeanne">
<cfif listContainsNoCase(list, "Ray")>
	There is a Ray in the list.
</cfif>

This code snippet will display a result since "Ray" partially matches "Raymond" in the list. In general you will probably never use listContains() since you almost always want to match an entire list item, not a partial one.

This question was written by Raymond Camden
It was last updated on January 18, 2006.

Categories

Data Structures

Comments

comments powered by Disqus