Skip to main content
Known Participant
September 16, 2008
Question

ListFind and ListContains

  • September 16, 2008
  • 1 reply
  • 2059 views
I have a string variable name "fieldName" and it output, "comments,other,active,sale".
I want to find if 'other' is in this string, so I use <cfif #ListFind(fieldName,"other")> but it does not work.

Does ListFind and/or ListContains actually find/search the variable for the desired output, or does it just find the first position and return true ?

I need to find out if 'other' is in the string, and if it is, I need to do one thing, if not, then do something else.
    This topic has been closed for replies.

    1 reply

    Inspiring
    September 16, 2008
    Doesn't work how? Your example has a syntax error. Is that it?
    Known Participant
    September 16, 2008
    No, it does not have a syntax error.

    I do something like this :
    <cfif #ListFind(fieldName,"other")>
    <cfoutput>Other has been found !</cfoutput>
    </cfif>

    But I do not get the cfoutput display, and I know that Other is in the list.

    That is why I say that it did not work, what am I doing wrong ?

    If Other is in the list by itself, then it works, if it is part of a list, it does not work.
    Inspiring
    September 17, 2008
    Olivia Crazy Horse wrote:
    > No, it does not have a syntax error.

    The example you posted does. It is missing a closing # sign. But from what you described, I assume that is just a typo.

    > Does ListFind and/or ListContains actually find/search the variable for the desired output,
    > or does it just find the first position and return true ?

    The documentation is the best resource for how the different functions operate:
    http://livedocs.adobe.com/coldfusion/7/htmldocs/00000552.htm
    Returns: Index of the first list element that contains value, with matching case. If not found, returns zero. The search is case-sensitive.

    > But I do not get the cfoutput display, and I know that Other is in the list.

    ListFind is performs case sensitive search. So if your list contains "Other", then searching for "other" (lowercase) will not find a match. To ignore case, use ListFindNoCase instead.

    Other things to be aware of are that any spaces in between the list elements can result in a failure to find a match. It is because the spaces are treated as part of the list element value.