ListFindNoCase problem with spaces

I ran into a problem today trying to find a person's userId value from a list of values using "listFindNoCase".

Here's the code:

<cfset allowedValues = "33333a, 44444b, 55555c, 66666d, 77777e, 88888f, 99999g">

<cfif listFindNoCase(allowedValues,session.userId)>
   This person is allowed in.
   This person is not allowed in.

Its difficult to see on this page, but there is a space after each comma in "allwedValues". This causes coldFusion to compare "33333a" to " 33333a", resulting in a false response. Removing the space between each value solved this, although it seems to be a bug to me.


<cfset allowedValues = "33333a,44444b,55555c,66666d,77777e,88888f,99999g">

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Christine Panus's Gravatar David,

I can see where you are coming from on this from a human readability standpoint. For people, lists have spaces so that we are better able to read them. However, spaces are just another character and all characters have meaning programmatically. When you do a list find with the default delimiter, the commas are removed and everything between the commas has value. So, while I can see why you would want, in this instance to ignore spaces, I do not agree that this is a bug. I'd rather have the ability to ignore spaces when I want to and include them when I need to rather than having the decision made for me and never be able to include spaces in my comparison. It is just as likely that in another application the space would have significance and need to be accounted for. If ColdFusion always ignored the trailing space, that option would not be available and that I think would be the bug (or a short sighted implementation).

Another way you could have accounted for the space, which you want to treat as part of the list delimiter is to specify the delimiter parameter value in the listFind function. By default the delimiter is comma. In this instance, you could set the delimiter parameter to space and comma. ColdFusion would then treat both the comma and the space as list delimiters. Remember, ColdFusion treats each character as a separate delimiter, rather than trying to pattern match the delimiters specified as one delimiter. Also, ColdFusion ignores empty list elements for the functions listFind and listFindNoCase, you'll end up with the list you are expecting. Ignoring empty list elements is what I would have termed a bug. However, as of ColdFusion 8, you can get around this using the listToArray function and setting the includeEmptyFields boolean value to true.

# Posted By Christine Panus | 9/2/08 4:00 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.