8

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(",");
2
  • 4
    Worth noting that .split() works in CF. I think all native Java string methods will work out of the box. <cfset a = "a,b,c" /> <cfdump var="#a.split( ',' )#" /> Commented Sep 6, 2011 at 20:29
  • 5
    Um... also probably worth noting that the resulting array is not a ColdFusion array, but a native Java array. You won't be able to modify it via CF. Henry's listToArray() is certainly the "most right" answer, but figured it wouldn't hurt to know about the native Java string methods working on CF strings (with the aforementioned caveat) :) Commented Sep 6, 2011 at 20:33

2 Answers 2

29
var a = ListToArray("a,b,c,d,e,f");     

https://cfdocs.org/listtoarray

Sign up to request clarification or add additional context in comments.

2 Comments

I dont understand this. I want to split by "," but you haven't defined that anywhere. How does the method know to use a "," as a delimiter? Also the documentation states it takes a list not a string. And this is supposed to be within a cfscript tag i assume?
@dlampard, because a list in CF is separated by commas by default.
3

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.

  • By default ColdFusion list functions ignore empty list items.
  • As of ColdFusion version 8, listToArray takes an optional third argument, includeEmptyFields, which is a boolean controlling that behavior, defaulting to false.

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:

  • CFDumps show it as an array
  • It's 1-based
  • You can use arrayLen on it
  • You can modify its elements in ColdFusion
  • There may be other behaviors I didn't check that aren't like a CF array, but as above, it's unsupported

In Railo:

  • Debug dumps show it as Native Array (java.lang.String[])
  • The other statements about its very array-like behavior are still true

That's in contrast to real java arrays, created with createObject("java", "java.util.ArrayList").
NOTE: That's only partly correct; see edit below.

  • For instance, in Adobe ColdFusion, elements of a java ArrayList can't be modified directly with CFML
  • In general, Railo handles java arrays more like ColdFusion ones than ACF

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.

1 Comment

java arrays, created with createObject("java", "java.util.ArrayList") An 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.