Splitting a String

I am trying to find a function in Coldfusion that is equivalent to the PHP explode function.

I have a string theDate = "7/3/04" and I want to seperate it by "/" into an array so that the first position in the array will be "7" then second will be "3" and the third will be "04". Can anyone give me a code example of how I can do this?
LVL 3
thecode101Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
eclipse2kConnect With a Mentor Commented:
Proper solution:

<cfset result= ListToArray(theDate, "/")>
<cfset Month1 = result[1]>
#Month1#
0
 
eclipse2kCommented:
You can handle it like it would be a list, but not delimited with , but with /, for example:

<cfset result = ListToArray(theDate, "/")>

You can also use

<cfset secondValue = ListGetAt(theDate, 2, "/")>


eclipse2k
0
 
thecode101Author Commented:
I tried this but it is not working.

<cfset result= ListToArray(#theDate#, "/")>
<cfset Month1 = result[0]>
#Month1#

returns the error:
The element at position 1 of dimension 1, of array variable &quot;RESULT,&quot; cannot be found.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
anandkpCommented:
<cfset result= ListToArray(theDate, "/")>
0
 
eclipse2kCommented:
In Coldfusion, Arrays start at Position 1, not 0!
0
 
anandkpCommented:
oops sorry - i didnt see that carefully ...

<cfset result=ArrayNew()>
<cfset result= ListToArray(theDate, "/")>

#result[0]#
0
 
anandkpCommented:
yeah eclipse is correct - [0] wont work with CF it starts from [1] ... gosh its time for me to go home ... :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.