CFLib.org – Common Function Library Project

findAll(substring, string [, start])

Last updated July 29, 2008

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

 
Rated 0 time(s). Average Rating: 0

Description:
Finds all occurrences of a substring in a string, from a specified start position. The search is case-sensitive. Returns an array of each starting position.

Return Values:
Returns an array.

Example:

view plain print about
<cfset mySTR = "123445566445566123445566123">
<cfset thisSTR = findAll("4455",mySTR)>
<cfdump var="#thisSTR#"/>

Parameters:

Name Description Required
substring String to search for. Yes
string String to parse. Yes
start Starting position. Defaults to 1. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Finds all occurrences of a substring in a string.
 * Fix by RCamden to make start optional.
 * 
 * @param substring      String to search for. (Required)
 * @param string      String to parse. (Required)
 * @param start      Starting position. Defaults to 1. (Optional)
 * @return Returns an array. 
 * @author Jeremy Rottman (rottmanj@hsmove.com) 
 * @version 2, July 29, 2008 
 */

function findALL(substring,string) {
    var findArray = arrayNew(1);    
    var start = 1;    
    var posStart = "";
    var i = 1;
    var newPos = "";
    
    if(arrayLen(arguments) gte 3) start = arguments[3];

    posStart = find(substring,string,start);
    
    if(posStart GT 0){
        findArray[i] = posStart;
        while(posStart gt 0 ){
            posStart = posStart + 1;
            newPos = find(substring,string,posStart);
            if(newPos gt 0){
                i = i + 1;
                findArray[i] = newPos;
                posStart = newPos;
            }else{
                posStart = 0;
            }
        }
    }else{
        return 0;
    }
    return findArray;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson