CFLib.org – Common Function Library Project

listAddAnd(list[, delimiter])

Last updated July 07, 2003

author

Deanna Schneider

Version: 2 | Requires: CF5 | Library: StrLib

Description:
Accepts lists with any delimiter, and returns a list with elements separated by a comma-space. Inserts an "and" before the last list element.

Return Values:
Returns a string.

Example:

<cfset mylist = "1,2,3">
<cfset myotherlist = "A-B-C">
<cfset mylist2 = "1,2">
<cfset mylist3 = "1">
<cfoutput>
#listAddAnd(mylist)#<br>
#listAddAnd(myotherlist, "-")#<br>
#listAddAnd(mylist2)#<br>
#listAddAnd(mylist3)#<br>
</cfoutput>

Parameters:

Name Description Required
list List to format. Yes
delimiter Delimiter to use. Defaults to a comma. No

Full UDF Source:

/**
 * Returns a list as an &quot;English language&quot; list.
 * 
 * @param list      List to format. (Required)
 * @param delimiter      Delimiter to use. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Deanna Schneider (deanna.schneider@ces.uwex.edu) 
 * @version 2, July 7, 2003 
 */
function listAddAnd(list) {
  var i = 1;
  var delimiter = ',';
  var returnValue = '';
  if(ArrayLen(arguments) GTE 2)
    delimiter = arguments[2];
  list = ListToArray(list, delimiter);
  if(arrayLen(list) eq 1) return list[1];
  if(arrayLen(list) eq 2) return list[1] & ' and ' & list[2];

  for(i = 1; i LTE ArrayLen(list); i = i + 1)
  if (i LTE (Arraylen(list) - 1)) returnValue = returnValue & list[i] & ', ';
  else returnValue = returnValue &   ' and ' & list[i];
  return returnValue;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson