CFLib.org – Common Function Library Project

ListCountSubstring(lst, str[, delim])

Last updated June 03, 2003

author

John King

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Modified from Peini Wu's CountIt(). This version works on lists.

Return Values:
Returns a number.

Example:

<CFSET LST = "trip,trim,trombone">
<CFOUTPUT>
LST = #LST#<BR>
ListCountSubstring(LST,"tr") = 
#ListCountSubstring(LST,"tr")#<BR>
ListCountSubstring(LST,"tri") = 
#ListCountSubstring(LST,"tri")#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
lst List to search. Yes
str Substring to search for. Yes
delim List delimiter. Defaults to a comma. No

Full UDF Source:

/**
 * Counts the number of list elements that contain a given substring.
 * 
 * @param lst      List to search. (Required)
 * @param str      Substring to search for. (Required)
 * @param delim      List delimiter. Defaults to a comma. (Optional)
 * @return Returns a number. 
 * @author John King (arocheking@yahoo.com) 
 * @version 1, June 3, 2003 
 */
function ListCountSubstring(lst, str) {
  var pos = 1;
  var count = 0;
  var newlst = "";
  var delim = ",";

  if(arrayLen(arguments) gte 3) delim = arguments[3];

  newlst = lst; //list to work on
  while(pos neq 0){
    pos = listContainsNoCase(newlst, str, delim);
    if (pos neq 0){ 
      newlst = listDeleteAt(newlst,pos,delim);
      count = count + 1;
    }
  }
  return count;
}

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