CFLib.org – Common Function Library Project

ListWrap(lst, length[, br][, delimiter])

Last updated July 15, 2013

author

Tony Brandner

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Takes in a list, a length, and an optional string and delimiter. It parses through the list, inserting "string" in such a way that the list will appear to be wrapped every "length" elements. Meant to be used with break strings like '<br>'.

Return Values:
Returns a string.

Example:

<cfset TempList = "a,b,c,d,e,f,g,h,i,j">
<cfoutput>
List:<br>#TempList#<br>
Wrapped List:<br>#ListWrap(TempList,4,"<br>",",")#
</cfoutput>

Parameters:

Name Description Required
lst The list to modify. Yes
length The place to do insertions. Yes
br String to insert. Detauls to a break tag. No
delimiter The delimiter to use. Defaults to a comma. No

Full UDF Source:

/**
 * Wraps a list every n elements.
 * 
 * @param lst      The list to modify. (Required)
 * @param length      The place to do insertions. (Required)
 * @param br      String to insert. Detauls to a break tag. (Optional)
 * @param delimiter      The delimiter to use. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Tony Brandner (tony@brandners.com) 
 * @version 1, July 15, 2013 
 */
function ListWrap(lst, lngth) {
    var newList=lst;
    var br="<br>";
    var delimiter=",";
        var i = lngth;
    // check for optional arguments
    if(ArrayLen(arguments) eq 3) {
        br = arguments[3];
    } else if (ArrayLen(arguments) EQ 4) {
        br = arguments[3];
        delimiter = arguments[4];
    }
    // loop through list
    for (i=lngth; i LE ListLen(lst,delimiter); i=i+lngth) {
        if (ListLen(lst, delimiter) GT i) {
            // append the break string to the next element
            newList = ListSetAt(newList, i+1, br & ListGetAt(lst, i+1, delimiter), delimiter);
        }
    }
    return newList;
}

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