CFLib.org – Common Function Library Project

CountIt(str, c)

Last updated October 17, 2003

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

 
Rated 4 time(s). Average Rating: 4.0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
15 day(s) ago

James Sleeman James Sleeman added
quickSort
25 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
28 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson