– Common Function Library Project

getOccurrenceCount(content, countThis[, countThisDelimiter])

Last updated October 13, 2006


Cody Ridgway

Version: 1 | Requires: CF5 | Library: StrLib

Count the occurences of a value or list of values in a string. Pass the string (lists will be treated as strings) to parse and anything from a single character to a list of various items to count and the count or list of counts will be returned. Spaces in the item or list of items are not stripped.

Return Values:
Returns a string.


<cfset testString = "<b>This is a test string</b> with <i>text</i>, tags, || (pipes), and a      (tab) in it.">

#getOccurrenceCount(testString,"., ,<b>,<,text,#Chr(9)#")#<br>
#getOccurrenceCount(testString,",|text|#Chr(9)#", "|")#<br>


Name Description Required
content String to parse. Yes
countThis Character, or list of characters to count. Yes
countThisDelimiter Delimiter. Defaults to a comma. No

Full UDF Source:

 * Count the occureneces of a value or list of values in a given string.
 * @param content      String to parse. (Required)
 * @param countThis      Character, or list of characters to count. (Required)
 * @param countThisDelimiter      Delimiter. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Cody Ridgway ( 
 * @version 1, October 13, 2006 
function getOccurrenceCount(content, countThis) {
    var countThisDelimiter = ','; 
    var countThisLen = 1; 
    var countThisItem = ';
    var returnCount = ';
    var i = 1;
    if(arrayLen(Arguments) GT 2) countThisDelimiter = Left(arguments[3],1);
    countThisLen = ListLen(countThis, countThisDelimiter);
    if(countThisLen GT 1) {
        for(i=1; i LTE countThisLen; i=i+1){
            countThisItem = listGetAt(countThis, i, countThisDelimiter);
            returnCount = ListAppend(returnCount, val(len(content) - len(replace(content,countThisItem,"","all")))/Len(countThisItem));
        returnCount = val(len(content) - len(replace(content,countThis,"","all")))/Len(countThis);
    return returnCount;


Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson