Thursday, September 30, 2010

listFind vs. listContains

ColdFusion has two functions for finding things in comma-delimited lists, but they behave very differently, and I can never which is which.  Today I needed one of them, and so I did a test to figure out which I needed.  If figured I would document their usages here, so that I wouldn't have to go thru this testing process next time.  Here are the functions:

listContainsNoCase(list, substring)
This function just finds the substring, and tells you which item contains that sub string.  It is important to understand that this item is not searching for a list item, just for a sub string.  Consider the following example:
listContainsNoCase("Date,Decimal,Image,Integer,Long Text,Rich Text,Simple Text", "Text")=5
Notice how it found that item number 5 has the word "Text" in it, even though "Text" is not the complete item.

listFindNoCase(list, value)
This function finds a value in a list, and tells you which item matches the value.  It compares the value to the list item, and only accepts complete matches.  So, consider the following examples:
listFindNoCase("Date,Decimal,Image,Integer,Long Text,Rich Text,Simple Text", "Text")=0
listFindNoCase("Date,Decimal,Image,Integer,Long Text,Rich Text,Simple Text", "Simple Text")=7
Notice how in the first example above, no item was found.  This is because there is no single item with value = "Text".  However, it did find "Simple Text" in the second example above.

Of course the same is true for listFind, and listContains, but those are case-sensitive.

Incidentally, I put this one in the "duh" category, because I think that the names are reversed.  The first usage should be named listFind, and the second should be named listContains.

Hope this helps.

0 comments: