Posted in ColdFusion | Posted on 05-02-2007 | 5,378 views
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.


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("__+__") >
<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.
Felix - it isn't built into CF, but a part of Java. I showed the UDF so folks could see a CF solution.
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...
str.split("\*\+\*")
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.
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.
see comment at http://cfblog.com/cgriefer/index.cfm/id/make_like_...
...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]