CFLib.org – Common Function Library Project

RFind(Substr, String[, SPos])

Last updated February 14, 2002

author

Charles Naumer

Version: 2 | Requires: CF5 | Library: StrLib

Description:
Returns the last index of an occurrence of a substring in a string from a specified starting position. (The reverse of find).

Return Values:
Returns the last position where a match is found, or 0 if no match is found.

Example:

<CFSET STR = "//// This is the test, the / test of all tests.">
<CFSET STR2 = "Ray's World">
<CFOUTPUT>
Test: #RFind("/",STR)#<BR>
Test2: #RFind("/",STR2)#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
Substr Substring to look for. Yes
String String to search. Yes
SPos Starting position. No

Full UDF Source:

/**
 * Returns the last index of an occurrence of a substring in a string from a specified starting position.
 * Big update by Shawn Seley (shawnse@aol.com) -
 * UDF was not accepting third arg for start pos 
 * and was returning results off by one.
 * Modified by RCamden, added var, fixed bug where if no match it return len of str
 * 
 * @param Substr      Substring to look for. 
 * @param String      String to search. 
 * @param SPos      Starting position. 
 * @return Returns the last position where a match is found, or 0 if no match is found. 
 * @author Charles Naumer (cmn@v-works.com) 
 * @version 2, February 14, 2002 
 */
function RFind(substr,str) {
  var rsubstr  = reverse(substr);
  var rstr     = "";
  var i        = len(str);
  var rcnt     = 0;

  if(arrayLen(arguments) gt 2 and arguments[3] gt 0 and arguments[3] lte len(str)) i = len(str) - arguments[3] + 1;

  rstr = reverse(Right(str, i));
  rcnt = find(rsubstr, rstr);

  if(not rcnt) return 0;
  return len(str)-rcnt-len(substr)+2;
}

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