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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson