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;
}
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