CFLib.org – Common Function Library Project

FindOccurrences(tString, tsubString)

Last updated March 20, 2002

author

Shawn Seley

Version: 3 | Requires: CF5 | Library: StrLib

Description:
Returns the number of times a pattern exists within a string.

Return Values:
Returns the number of occurrences.

Example:

<CFSET TestString = "this is a test of this function">
<CFOUTPUT>"is" occurs #FindOccurrences(TestString,"is")# times in TestString</CFOUTPUT>

Parameters:

Name Description Required
tString The string to check. Yes
tsubString The string to look for. Yes

Full UDF Source:

/**
 * Returns the number of times a pattern exists within a string.
 * Modified by Raymond Camden
 * Rewritten based on original UDF by Cory Aiken (corya@fusedsolutions.com)
 * 
 * @param tString 	 The string to check. 
 * @param tsubString 	 The string to look for. 
 * @return Returns the number of occurrences. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 3, March 20, 2002 
 */
function FindOccurrences(tString,tsubString){
	if(not len(tString) OR not len(tsubString)) return 0;
	else {
		// delete all occurences of tString
		// and then calculate the number of occurences by comparing string sizes
		return ((len(tString) - len(replaceNoCase(tString, tsubString, "", "ALL"))) / len(tsubString));
	}
}
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