Coldfusion: How to split a string into a set of variables
Asked Answered
A

2

18

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

Appulse answered 15/5, 2014 at 0:11 Comment(3)
How many elements were in your list?Glinys
you could also do replace(myList,'<br>',',','all') to turn it into a true list and then you don't have to rely on underlying Java methodsQuimper
"a true list" - that's a nonsense phrase; there is no "true" list. Comma is merely the default delimiter for the List~ string manipulation functions.Sick
A
29

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.

Annabal answered 15/5, 2014 at 0:19 Comment(13)
It looks like Split is a Java function. It is interesting that a is in fact a CF array of strings and not some sort of Java objectMons
No need for java functions in this case. CF9+ supports multi-character delimiters: listToArray(..., multiCharacterDelimiter).Adamis
@JamesAMohler - Actually it is a java object: String[]. Though it is similar to a CF array, and can be used with most CF array functions, there are some slight differences. Unlike a CF array, it is immutable, so you cannot append or remove elements.Adamis
Jack - If you are just learning CF, you may want to skip the java stuff for now. Focus on learning the core functions first. I usually recommend people read the API carefully before using the underlying java functions. The split() method accepts a regular expression (not just a plain string) so certain characters need to be escaped. Also, unlike most CF functions, it is case sensitive. The differences catch some people unawares and then they are scratching their heads at the unexpected results.Adamis
I don't know that I would tell him to skip the Java functions completely, especially the String functions. The Java String functions are a big part of what makes Java so powerful in CF. Plus, they are usually a lot faster than the CF functions. Throwing a little bit of Java into the mix will make CF a little bit more complex to learn, but I think it will overall make him a more well-rounded CF developer.Epigastrium
Nothing wrong with learning about Java's String methods from the start - at least knowing they exist, do some things differently, and do some things native CFML doesn't. Using them without knowing the differences is a mistake, but they are a useful tool - more so than some core functionality!Sick
I obviously agree with Shawn, except for "they are usually a lot faster than the CF functions" - that's not always true - they can be faster but can also be slower, (and in both cases whether the difference is significant varies).Sick
@PeterBoughton - RE: Nothing wrong with learning about Java's String methods from the start I don't disagree with that, but too often java is the first or only suggestion, regardless of whether it is even necessary. The question is about learning cfml, yet the original answer made no mention of the native cfml function designed for exactly that purpose ;) Surely it should at least be mentioned first. Yes, java is a great tool, but it is not always the right one. (Case in point, why use java here when cfml already provides adequate functionality?) cont'd...Adamis
Discuss the options (plural), but also provide a brief explanation ie When/why you might use java method x, instead of cfml function y, the differences between, etcetera. Ultimately, providing a broader explanation does a lot more to expand someone's knowledge than just saying "use java...". </petPeeve> BTW, I am not slamming the answer, just saying the original could have provided a more balanced view IMO.Adamis
@Epigastrium - I did not say skip completely, just learn the core functions first. Hard to be well rounded if you know java, but not the core functions in your current language: cfml ;-) Personally, I love java, but it is not always the faster or even the right tool for every job. I'm just suggesting he get a good handle on CF's capabilities first, so he can make an informed decision about when/how to use java effectively.Adamis
When there's a specifically designed function and it's basically an update to what the user tried, then yes that should obviously be the first thing an answer mentions. Beyond that... CFML is a JVM language. Whether functionality comes from Railo/CF or from the JRE class library shouldn't matter (once it does what it needs to do). What you're saying seems akin to "learn all (FW/1|Coldbox|etc) functionality first, then learn CFML" - as opposed to simply learning them together, and contrasting their capabilities when there's overlap.Sick
RE: contrasting their capabilities There is nothing "to" contrast if they do not even learn cfml. RE: What you're saying seems akin to Lol, I knew that that "skip java" comment would come back to haunt me. It was late when I posted, and I tried to clarify it in later comments. I am not saying "do not learn java...", but rather do not neglect to learn cfml, just because you can do some things in java. Some one reading the original post would walk away thinking cfml did not have this capability.Adamis
@Adamis Just found that ListToArray() when using multiCharacterDelimiter is also case-sensitive in CF11 latest update.Willock
D
2
<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,'|')>
Decoct answered 15/5, 2014 at 12:40 Comment(2)
Downvote for "unlikely" - you don't know what the description might contain! Also there are ASCII characters dedicated to delimiting that could be used instead, if it was necessary to do delimiter swapping (it isn't).Sick
Sorry if I wasn't clear. I was pointing out the character to indicate it could be changed. I was trying to make for a simple example. Leigh mentioned a preference for an answer that didn't use Java. I was trying to provide that.Decoct

© 2022 - 2024 — McMap. All rights reserved.