Created
March 18, 2026 18:43
-
-
Save trycf/d2582e1b965a62ad1c3f2f346e4c8242 to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfsetting requestTimeout="10"> | |
| <cfscript> | |
| try { | |
| result = countOccurences("hello", ""); | |
| writedump(result); | |
| } catch(e) { | |
| writedump(e); | |
| } | |
| </cfscript> | |
| <cffunction name="countOccurences" access="public" returntype="numeric" output="false"> | |
| <cfargument name="cText" type="string" required="true" /> | |
| <cfargument name="cTextToFind" type="string" required="true" /> | |
| <cfargument name="bCaseSensitive" type="boolean" required="false" default="false" /> | |
| <cfset var iOccurences = 0 /> | |
| <cfset var iFoundPos = 0 /> | |
| <cfset var iStartPos = 1 /> | |
| <cfset var bFindNext = true /> | |
| <cfloop condition="#bFindNext#"> | |
| <cfset iFoundPos = _find(arguments.cTextToFind, arguments.cText, iStartPos, arguments.bCaseSensitive) /> | |
| <cfif iFoundPos GT 0> | |
| <cfset iStartPos = iFoundPos + len(cTextToFind) /> | |
| <cfset iOccurences = iOccurences + 1 /> | |
| <cfelse> | |
| <cfset bFindNext = false /> | |
| </cfif> | |
| </cfloop> | |
| <cfreturn iOccurences /> | |
| </cffunction> | |
| <cffunction name="_find" access="private" returntype="numeric"> | |
| <cfargument name="cSubString" type="string" required="true" /> | |
| <cfargument name="cString" type="string" required="true" /> | |
| <cfargument name="iStart" type="string" required="true" /> | |
| <cfargument name="bCaseSensitive" type="boolean" required="false" default="false" /> | |
| <cfif arguments.bCaseSensitive> | |
| <cfreturn find(arguments.cSubString, arguments.cString, arguments.iStart) /> | |
| </cfif> | |
| <cfreturn findNoCase(arguments.cSubString, arguments.cString, arguments.iStart) /> | |
| </cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment