How to make multi character delimiter in Coldfusion for cfloop?
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
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.
Use .split() function instead.
For example, you can split a string by a string delimiter like this:
"string&^&string&^&string".split("&^&")
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>
精彩评论