CFLib.org – Common Function Library Project

CountIt(str, c)

Last updated October 17, 2003

author

Peini Wu

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Get a count on searching string in the searched string. Simular to ListLen, however this UDF accepts a string, not a single character, as a delimiter.

Return Values:
Returns a numeric value.

Example:

<CFSET STR = "what a world what a world what a world">
<CFOUTPUT>
STR=#STR#<BR>
CountIt(STR,"what a world") = 
#CountIt(STR, "what a world")#<BR>
CountIt(STR,"world") = 
#CountIt(STR, "world")#<BR>
CountIt(STR,"d") = 
#CountIt(STR, "d")#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
str The string to search. Yes
c The string to look for. Yes

Full UDF Source:

/**
 * Get a count on searching string in the searched string.
 * Updated by Raymond Camden
 * 
 * @param str      The string to search. (Required)
 * @param c      The string to look for. (Required)
 * @return Returns a numeric value. 
 * @author Peini Wu (pwu@hunter.com) 
 * @version 1, October 17, 2003 
 */
function CountIt(str, c) {
    var pos = findnocase(c, str, 1);
    var count = 0;

    if(c eq "") return 0;
    
    while(pos neq 0){
        count = count + 1;
        pos = findnocase(c, str, pos+len(c));
    }
    
    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