CFLib.org – Common Function Library Project

FindOccurrences(tString, tsubString)

Last updated March 20, 2002
Download UDF

author

Shawn Seley                                       Shawn Seley

Version: 3 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

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:

<cfscript>
/**
* 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 (corya@fusedsolutions.comshawnse@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));
    }
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson