Skip to main content
June 19, 2006
Answered

Multiple Chr. Delimiters

  • June 19, 2006
  • 3 replies
  • 1438 views
Is there a way to use multiple characters as a delimiter? For instance:

<cfloop index="i" list="1 : 2 : 3 : 4" delimiters=" : "></cfloop>

If not -- how would you do this. We have data comming from a third party and it has some inconsistent data. If we can use multiple delimiters then it will solve the problem.
    This topic has been closed for replies.
    Correct answer
    Thanks everyone -- got it!

    3 replies

    Inspiring
    June 19, 2006
    <cfscript>
    theList="1 : 2 : 3 : 4 : 5 : test:meElmo : duck:and:cover";
    mcRegex="\s:\s";
    pattern=createObject("java","java.util.regex.Pattern").compile(mcRegex);
    parsedArray=pattern.split(theList);
    </cfscript>

    <cfdump var="#parsedArray#">
    Correct answer
    June 19, 2006
    Thanks everyone -- got it!
    Inspiring
    June 19, 2006
    > You can put more than one character in the delimiters attribute, in any
    > order. For example, this loop processes commas, colons, and slashes as list
    > delimiters:

    I didn't take the requirement to mean "more than one delimiter", but to be
    "one multi-character delimiter"..?

    CF does not support multi-character delimiters natively. You can use the
    Java split() method to split your list into an array, and then loop over
    the array.

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

    --
    Adam
    Participating Frequently
    June 19, 2006
    Per live docs at cfloop: looping over a list or file

    You can put more than one character in the delimiters attribute, in any order. For example, this loop processes commas, colons, and slashes as list delimiters:

    <cfloop index = "ListElement"
    list = "John/Paul,George::Ringo"
    delimiters = ",:/">
    <cfoutput>#ListElement#</cfoutput><br>
    </cfloop>


    Phil
    June 19, 2006
    What if you need the delimiter to be " : "? Here is an example of the data we are dealing with:

    Cards: US : Paper : Trash

    "Cards: US is the first item. Not Cards and then US. There are only 3 items in this list NOT 4. If we use ":" as the delimiter it would appear to be 4 different items.
    Inspiring
    June 19, 2006
    You may want to use a different separator then, because a delimiter is a single character. You can define multiple delimiters for the command to look for, but the delimiter still has to be a single character.

    We usually use the pipe (|) because it is a non-standard character that you won't find cropping up in everyday strings, like colons or commas do.