CFLib.org – Common Function Library Project

ListCountSubstring(lst, str[, delim])

Last updated June 3, 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;
}
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