CFLib.org – Common Function Library Project

lastIndexOf(targetString, lastChar)

Last updated August 16, 2008

author

Chris Douglas

Version: 2 | Requires: CF6 | Library: StrLib

Description:
This UDF returns the index position of the last matching character in a string that you are searching for. If no matching string is found, a 0 is returned.

Return Values:
Returns a number.

Example:

<cfset file_name = "test.txt">

<cfset fileExtensionPosition = len(FORM.file_name) - lastIndexOf(FORM.file_name, ".")>
<!--- fileExtensionPosition  = 4 --->

<cfset fileExt = "#removeChars(FORM.file_name, 1, fileExtensionPosition)#">
<!--- fileExt = .txt --->

Parameters:

Name Description Required
targetString String to check. Yes
lastChar Character to look for. Yes

Full UDF Source:

<!---
 Returns the index position of the last matching character in a string.
 v2 rewrite by Raymond Camden
 
 @param targetString 	 String to check. (Required)
 @param lastChar 	 Character to look for. (Required)
 @return Returns a number. 
 @author Chris Douglas (chrisgdouglas@gmail.com) 
 @version 2, August 16, 2008 
--->
<cffunction name="lastIndexOf" returntype="numeric" output="no">
	<cfargument name="targetString" type="string" required="yes">
	<cfargument name="lastChar" type="string" required="yes">
  
	<cfif find(lastChar, arguments.targetString)>
		<cfreturn len(arguments.targetString) - find(lastChar,reverse(arguments.targetString))>  
	<cfelse>
		<cfreturn 0>
	</cfif>
	
</cffunction>
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