Skip to main content
October 5, 2011
Question

ListContains() Function

  • October 5, 2011
  • 1 reply
  • 1535 views

I have a table with a column for listing colors.

The current comma delimited color list is: Orange & White, White

When adding a new color, I verify the color being added does not already exists. So I do a simple ListContains() function like this:

<CFSET v.vendorcolors = "Orange & White, White">

<CFSET imprintcolor = "Orange">

<CFIF ListContains(v.vendorcolors,imprintcolor,",")>

<CFSET message = "This color already exists.">

<CFELSE>

<CFSET form.vendorcolors = "#ListAppend(v.vendorcolors,imprintcolor)#">

</CFIF>

As you can see, if it doesn't exist in the list, then I APPEND it to the list.

Now, this was working great until I tried to add "ORANGE" as color.

You can see I have a "Orange & White" color options, but not just "Orange".

When I run this IF statement above, it says the list already contains the color "Orange". If I am telling it to use a comma as the delimiter, then why is it saying it exists?

Chuck

This topic has been closed for replies.

1 reply

12Robots
Participating Frequently
October 5, 2011

You want to use ListFind(). ListContains() is a wildcard search. When you use ListContains() you are searching any values for *Orange* so it will return true for "Orange", "Orange & White", "Orangeapotomus", "Orange Julius", "Orange you glad I didn't say Banana?", and anything with "Orange" in it. Note the casing. You might also want to look at ListFindNoCase().

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d84.html

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d80.html

October 5, 2011

Why would they offer a "Delimiter" option for ListContains() if its going to totally ignore it?

I guess I will use ListFind().

Chuck

Inspiring
October 5, 2011

Re-read Jason's response. It does not ignore the delimiter. You are just misunderstanding how ListContains works