Variable Type Gotchas - ColdFusion List Delimeters

During my presentation earlier this week, I brought up a few 'Gotchas' that ColdFusion developers may not be aware of. Today I'm going to review some of these for those who couldn't make the presentation.

The first item that came up was during my discussion of ColdFusion lists. The one thing I really see trip people up (and it tripped me up when I was building my first ColdFusion application - back in the dinosaur days) is the fact that list delimiters are only single characters - even if you pass multiple delimiters. So what do I mean? Consider this list:

<cfset monkeys = "King Kong__+__George, Bush__+__Clinton, Hillary__+__Super_Monkey">

Looking at this string you might think you could simply tell ColdFusion to use + as a delimiter. Consider this loop:

<cfloop index="item" list="#monkeys#" delimiters="__+__">

Many developers think that ColdFusion will do this: "Ok, take my string, and consider the literal string + as a delimiter." Instead, ColdFusion does this: "Ok, I'll use _ and + as delimiters."

What this means is that our loop will display:

King Kong
George, Bush
Clinton, Hillary
Super
Monkey

The _ inside of Super_Monkey was considered a delimiter and therefore was split up by ColdFusion. In case you do want to split up a string by a set of characters, consider the split() UDF at CFLib.

Archived Comments

Comment 1 by Felix Tjandrawibawa posted on 5/2/2007 at 5:08 PM

Hi Ray,

I've come across a situation where I need to do some string splitting myself. And I found that there's a split function built in Coldfusion (I'm using CF7).
So for splitting the monkeys string, I would just use:
&lt; cfset aMonkeys = myString.split("__+__") &gt;

Comment 2 by Phillip Senn posted on 5/2/2007 at 5:14 PM

<cfset Counter = 0>
<cfoutput>
<cfloop list="A,,C,D" index="I">
<cfset Counter = Counter + 1>
#Counter#: #I#<br />
</cfloop>
</cfoutput>

One might think Counter would be 4, but it only counts up to 3.

Comment 3 by Raymond Camden posted on 5/2/2007 at 5:20 PM

Geeze Phil, that was going to be my next post. ;) (I'll still post though.)

Felix - it isn't built into CF, but a part of Java. I showed the UDF so folks could see a CF solution.

Comment 4 by Seb Duggan posted on 5/2/2007 at 5:22 PM

Felix - bear in mind that this uses Java's underlying split() method, and that the parameter it takes is a regular expression.

So, if you wanted to split a string using "*+*" as the delimiter, you'd need to escape the regex special characters, e.g. str.split("/*/+/*").

Useful tip, though. I tend to forget about all the Java string methods you can call on...

Comment 5 by Seb Duggan posted on 5/2/2007 at 5:23 PM

Of course, that should be:

str.split("\*\+\*")

Comment 6 by Felix Tjandrawibawa posted on 5/2/2007 at 5:44 PM

Oops, didn't realised that split() is actually Java's split().

Comment 7 by Tom Mollerus posted on 5/2/2007 at 6:35 PM

Could someone please expand on the split() method of strings? Do you have to create the string in Java code to have the split method available? What other Java methods are available to strings in CF?

If the split() method is part of any string created in CF, and if the split method is available on any CF installation regardless of the Java installation, then'd I'd suggest that it be considered a "part" of Coldfusion as much as any other function.

Comment 8 by Seb Duggan posted on 5/2/2007 at 7:03 PM

Tom,

You don't need to create the code in Java. The following code:

<cfset strFruit = "apples--*--oranges--*--pears--*--bananas" />
<cfset arrFruit = strFruit.split("--\*--") />

Would create an array of 4 fruits. Notice the escaped asterisk in the split parameter.

Comment 9 by Tom Mollerus posted on 5/2/2007 at 7:14 PM

@Seb: I went to the bother (jk) of actually trying this out myself. You're right, it does work! So whether or not we label these methods as part of Java or part of ColdFusion doesn't matter so much. When can we use these methods and when can't we? Does it depend on the application server that CF is using (JRun, WebSphere, BEA, etc.) or does it depend on the JRE installed? Or are they always available?

Comment 10 by Seb Duggan posted on 5/2/2007 at 7:20 PM

I don't think there's a restriction on when you can use them. And I think all versions of CFMX run on Java, whatever platform they're on, so they should all have access to the Java methods...

Comment 11 by Tom Mollerus posted on 5/2/2007 at 7:21 PM

Ben Nadel has a posting (http://www.bennadel.com/blo... with a link to more information about using Java string methods in CF, as well as an argument that they're not quite as useful as CF methods (arrays created with the split() method are read-only).

Comment 12 by charlie griefer posted on 5/2/2007 at 7:21 PM

there's a gotcha on using split() that was recently brought to my attention. doesn't work on empty values at the end of a list.

see comment at http://cfblog.com/cgriefer/...

Comment 13 by David Harris posted on 5/3/2007 at 1:56 AM

Another idea I've seen when working with list...
...use a char that cannot be typed in when building your lists...
eg #chr(7)# (the "beep")

So you would have this:

cfset monkeys = "King Kong#chr(7)#George, Bush#chr(7)#Clinton, Hillary#chr(7)#Super_Monkey"

cfloop index="item" list="#monkeys#" delimiters="#chr(7)#"

I would suspect it is very VERY unlikely someone will put a chr(7) in there data! :-)