views:

225

answers:

2

I have a String variable which has dynamic user entered text

EX:- <cfset setPars="SPTO_DATE('04/11/2009 11:59:59 PM', 'MM/DD/YYYY HH:MI:SS AM')SP(L','MN)>'

Now If I use SP as the delimiter

in CFloop as below

    <cfloop index="i" from="1" To="#ListLen(setPars,'SP')#">
       <br/> #ListGetAT(setPars,i,'SP')# 
    </cfloop>

I am getting output As

TO_DATE('04/11/2009 11:59:59 

M', 'MM/DD/YYYY HH:MI: 

But I want as

TO_DATE('04/11/2009 11:59:59 PM', 'MM/DD/YYYY HH:MI:SS AM')

(L','MN)

Is there any way in Coldfusion to do that?

Thanks

A: 

Not using the built-in List*() functions, no. They all treat the delimiter argument as a set of characters, rather than a literal string. But you can, of course, use the regular string functions (e.g. Find(), Mid(), etc.) to do the parsing yourself.

Also, for what it's worth, your loop can be written more compactly (as long as you don't care about the numeric indices of each list item) as:

<cfloop index="i" list="#setPars#">
   <br/> #i#
</cfloop>
Sixten Otto
+4  A: 

There is not a direct way to do this. However, there are a couple of different ways to accomplish it.

What I usually do is replace the multichararacter delimiter with a single character. I usually use the bell (chr(7)) because it's not typable on a standard keyboard.

<cfset list = replace(setPars, 'SP', '#chr(7)#', 'all')>

Then, you can loop over the list:

<cfloop list="#list#" index="i" delimiters="#chr(7)#">
    <br />#i#
</cfloop>

Note the simpler loop operator. It will save you some work.

Ben Doom
I use CHR(30) (RECORD SEPARATOR) the same way. Seems less dangerous and the character was designed for just that. Personal preference, I guess.
Al Everett
There's also `ListChangeDelims`. Using your example: `<cfset list=ListChangeDelims(setPars, CHR(7) , "SP") />`
Al Everett