Caution: Java String::Split() Does Not Create A ColdFusion Array

Posted January 26, 2007 at 10:41 PM by Ben Nadel

Tags: ColdFusion

This has been covered before, but you can call the Java string methods on ColdFusion string objects. One of those methods is the String::Split() method. In the Java documentation, it says that it splits this string around matches of the given regular expression. The return value is an array of String objects. When dumped out, these arrays look just like ColdFusion arrays, but they are NOT ColdFusion arrays. ColdFusion arrays are actually Vectors.

To demonstrate that, here are two arrays, one built by ColdFusion and one built by Java:

  • <!--- Set up the words list. --->
  • <cfset strWords = "I like to move it, move it!" />
  •  
  • <!---
  • Create the words array using the underlying
  • Java method, String::Split().
  • --->
  • <cfset arrWordsJava = strWords.Split( " {1}" ) />
  •  
  •  
  • <!---
  • Create the words array using the ColdFusion
  • function ListToArray().
  • --->
  • <cfset arrWordsCF = ListToArray( strWords, " " ) />

Dumping out the Java String::Split() array, we see:


 
 
 

 
CFDump Java String::Split()  
 
 
 

Dumping out the ColdFusion ListToArray() array, we see:


 
 
 

 
CFDump ColdFusion ListToArray()  
 
 
 

They look exactly the same, right??? Maybe, but they are quite different. From what I can see, the Array returned by the Split() method is read only. If you look at the class methods:


 
 
 

 
CFDump - Java String::Split() Java Class Methods  
 
 
 

... you can see there are no methods about altering it. In fact, if you try to use ArrayResize() on this array, you get the error:

null null

... and if you try to update one of the values (as in arrWordsJava[ 1 ] = "Ben") you get the error:

You have attempted to dereference a scalar variable of type class coldfusion.runtime.Cast$1 as a structure with members.

Seems like this is read only. The Split() method is really awesome, especially since it splits on a Regular Expression, but just BE CAREFUL, the resultant array is pretty much only for reference.




Reader Comments

Jan 27, 2007 at 12:09 AM // reply »
48 Comments

I came acrossed this _exact_ issue yesterday. This explains it.

Consider the following code:

<cfset tzObj=createObject("java","java.util.TimeZone")>
<cfset tzs = tzObj.getAvailableIDs()>
<cfdump var="#tzs#">

<!--- uncomment the next line and you'll get the funky error --->
<!--- <cfset arraySort(tzs, "textnocase")> --->

<!--- rebuild the array manually and the arraySort() works....WEIRD! --->
<cfset foo = arrayNew(1)>
<cfloop from="1" to="#arrayLen(tzs)#" index="i">
<cfset arrayAppend(foo, tzs[i])>
</cfloop>
<!--- no error here --->
<cfset arraySort(foo, "textnocase")>
<cfdump var="#foo#">

So why to some of the array functions work? For example, arrayToList(tzs) works. I ultimately used arrayToList() and listSort() to achieve the desired result.

PS. Noob question - how did you dump the class methods?


Jan 27, 2007 at 1:37 PM // reply »
11,246 Comments

Todd,

Yeah, what you are saying makes sense. ArrayToList() must actually iterate over the array to get the value (hence it works with the read-only properties of the array). I guess it is not doing any kind of Join or anything like that.

As far as getting the class methods, I use reflection to get the information about the Java object (works also with ColdFusion objects). Take a look at the ColdFusion User Defined Function I wrote for this:

http://bennadel.com/index.cfm?dax=blog:206.view

It works 95% of the time. I have to add a try/catch in there for special cases, but so far it's been pretty great.


Jun 5, 2007 at 10:41 PM // reply »
1 Comments

Read Only !!! That explains why <cfparam name=array[i] wont work on an array created with split...Any other solutions?


Jun 7, 2007 at 9:41 AM // reply »
11,246 Comments

@Rob,

You inspired me to finally go think it out:

http://www.bennadel.com/index.cfm?dax=blog:760.view


Feb 3, 2009 at 2:19 PM // reply »
6 Comments

ack! this totally bit my arse this morning... grrrr

good to see I'm not the only one that has run into it..


Aug 11, 2009 at 1:45 PM // reply »
1 Comments

Thanks for the post,
I was looking for this piece of code for ages


Feb 6, 2012 at 2:18 PM // reply »
1 Comments

I know this blog is old, but arent coldfusion arrays just java lists? I dont think coldfusion arrays are really arrays.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools