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:
Looking at this string you might think you could simply tell ColdFusion to use __+__ as a delimiter. Consider this loop:
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.


Comment 1 written by Felix Tjandrawibawa on 2 May 2007, at 8:08 AM
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:
< cfset aMonkeys = myString.split("__+__") >
Comment 2 written by Phillip Senn on 2 May 2007, at 8:14 AM
<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 written by Raymond Camden on 2 May 2007, at 8:20 AM
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 written by Seb Duggan on 2 May 2007, at 8:22 AM
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 written by Seb Duggan on 2 May 2007, at 8:23 AM
str.split("\*\+\*")
Comment 6 written by Felix Tjandrawibawa on 2 May 2007, at 8:44 AM
Comment 7 written by Tom Mollerus on 2 May 2007, at 9:35 AM
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 written by Seb Duggan on 2 May 2007, at 10:03 AM
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 written by Tom Mollerus on 2 May 2007, at 10:14 AM
Comment 10 written by Seb Duggan on 2 May 2007, at 10:20 AM
Comment 11 written by Tom Mollerus on 2 May 2007, at 10:21 AM
Comment 12 written by charlie griefer on 2 May 2007, at 10:21 AM
see comment at http://cfblog.com/cgriefer/index.cfm/id/make_like_...
Comment 13 written by David Harris on 2 May 2007, at 4:56 PM
...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! :-)
[Add Comment] [Subscribe to Comments]