Is there an easy way to split a comma delimited string into an array using cfscript?
Something similar to the following JavaScript:
var a = "a,b,c".split(",");
var a = ListToArray("a,b,c,d,e,f");
Your two main options are listToArray(myList) and the java method myList.split(), as noted in previous answers and comments. There are some things to note though.
For example:
listToArray("asdf,,,qwer,tyui") is ["asdf", "qwer", "tyui"]
listToArray("asdf,,,qwer,tyui", ",", true) is ["asdf", "", "", "qwer", "tyui"]
Re java split:
Like other java functionality that pokes up through the ColdFusion layer, this is undocumented and unsupported
In Adobe ColdFusion 8, 9, and 10, but not in Railo, this is a syntax error:
a = "asdf,,,qwer,tyui".split(",")
But this works:
s = "asdf,,,qwer,tyui";
a = s.split(",");
As far as I can see, Adobe ColdFusion treats the result of .split() like a ColdFusion array:
In Railo:
That's in contrast to real java arrays, created with createObject("java", "java.util.ArrayList").
NOTE: That's only partly correct; see edit below.
Edit: Thanks Leigh, I stand corrected, I should stick to what I know, which is CF way more than java.
I was reacting to the comment saying that the result of .split() "is not a ColdFusion array, but a native Java array. You won't be able to modify it via CF", which is not the case in my experience. My attempt to clarify that by being more specific was ill-informed and unnecessary.
ArrayList is not the same as a java array. ArrayList's are a type of java.util.List and can be modified, same as any CF array. (Beneath the hood, ACF "arrays" are java.util.List's too). A "real" java array is different. A java array's size is immutable. Meaning you can modify its elements, but you cannot perform operations that change its size ie add or remove.
<cfset a = "a,b,c" /> <cfdump var="#a.split( ',' )#" />