CFLib.org – Common Function Library Project

findAll(substring, string[, start])

Last updated July 29, 2008

author

Jeremy Rottman

Version: 2 | Requires: CF5 | Library: StrLib

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:

<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:

/**
 * 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;
}

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