listToArray

Copies the elements of a list to an array.

listToArray(list [, delimiters] [, includeEmptyFields] [, multiCharacterDelimiter]) → returns array

Member Function Syntax

str.listToArray([delimiters] [, includeEmptyFields] [, multiCharacterDelimiter])

Argument Reference

list string
Required

A list or variable name

delimiters string
Default: ,

includeEmptyFields boolean
Default: false

A Boolean value specifying whether to create empty array entries if there are two delimiters in a row.
Values:
  • true
  • false

multiCharacterDelimiter boolean
Default: false

A Boolean value specifying whether the delimiters parameter specifies a multi-character delimiter.
Values:
  • true
  • false

Examples
Sample code invoking the listToArray function

Uses the listToArray() function to retrieve a list as an array

list = "red,green,orange";
getArray = listToArray(list);
someJSON = serializeJSON(getArray);
writeOutput(someJSON);

Expected Result: ["red", "green", "orange"]

Uses the listToArray() function with a semicolon delimiter to retrieve a list as an array

list = "coldfusion;php;java;sql";
getArray = listToArray(list,";");
someJSON = serializeJSON(getArray);
writeOutput(someJSON);

Expected Result: ["coldfusion", "php", "java", "sql"]

If includeEmptyFields is true, empty value add in array elements

list = "coldfusion;php;;java;sql";
getArray = listToArray(list,";",true);
someJSON = serializeJSON(getArray);
writeOutput(someJSON);

Expected Result: ["coldfusion", "php", " " , "java", "sql"]

Uses the listToArray() function to retrieve a list as an array with multiCharacterDelimiter

list = "coldfusion,php,|test,java,|sql";
getArray = listToArray(list,",|",false,true);
someJSON = serializeJSON(getArray);
writeOutput(someJSON);

Expected Result: ["coldfusion,php", "test,java", "sql"]

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub