CFLib.org – Common Function Library Project

listAddAnd(list[, delimiter])

Last updated July 7, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson