Pergunta

I'm trying to teach myself ColdFusion.

I have a string coming in from the database in this format:

domain.com
<br/>
www.facebook.com/facebookpage
<br/>
http://instagram.com/instagrampage

It is all coming from #getRacer.txtDescription#. The format of this text will always be the same.

I need to split it into 3 variables. I tried this (derived from the example on the adobe website)

<h3>ListToArray Example</h3>
<cfset myList = ValueList(getRacer.txtDescription)>
<p>My list is a list with <cfoutput>#ListLen(myList)#</cfoutput> elements.
<cfset myArrayList = ListToArray(myList,'<br/>')>
<p>My array list is an array with 
<cfoutput>#ArrayLen(myArrayList)#</cfoutput> elements.

I somehow ended up with 11 items in the array.

Thank you

Foi útil?

Solução

This should work.

<cfset TestSTring = "domain.com<br/>www.facebook.com/facebookpage<br/>http://instagram.com/instagrampage">

<cfset a = TestString.Split("<br/>")>

The reason ListtoArray is displaying 11 items is because ColdFusion treats each character in the delimiter string (<br/>) as a separate delimiter

Based on @Leigh's comment updating my answer to ensure people should learn the Coldfusion APIs rather than fiddling with Java functions, <cfset a = ListToArray(TestString, "<br/>", false, true)> will also work. Thanks Leigh.

Note: The false at the end is for the includeEmptyFields flag and the true is for the multiCharacterDelimiter flag. See the docs.

Outras dicas

<cfset myList = ReplaceNoCase(getRacer.txtDescription,'<br/>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>

I chose a pipe character because it is unlikely to already exist in your string. If you wanted to account for the possibility that your BR tag may or may not use XML syntax then you could you regex:

<cfset myList = ReReplaceNoCase(str,'<br/?>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top