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

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