CFLib.org – Common Function Library Project

ListFindReplace(string, listToMatch, listToReplace[, delim])

Last updated August 4, 2005

author

BenJamin Prater

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Finds a value in one list and returns the matching string at the same index in another list. Useful when a database stores values in a different way than you want to display and you don't want a series of cfcase statements for the display values.

Return Values:
Returns a string.

Example:

<cfset database_return_value = "inactive">
<cfoutput>
#listFindReplace(database_return_value, "active,inactive,deleted", "Account Active,Account Inactive,Account Closed")#
</cfoutput>

Parameters:

Name Description Required
string List to parse. Yes
listToMatch List of terms to match. Yes
listToReplace Matching list of words to use as replacements. Yes
delim List delimiter. Defaults to a comma. No

Full UDF Source:

/**
 * Finds a value in one list and returns the matching string at the same index in another list.
 * 
 * @param string 	 List to parse. (Required)
 * @param listToMatch 	 List of terms to match. (Required)
 * @param listToReplace 	 Matching list of words to use as replacements. (Required)
 * @param delim 	 List delimiter. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author BenJamin Prater (bprater@bluefoxlabs.com) 
 * @version 1, August 4, 2005 
 */
function listFindReplace(string, listToMatch, listToReplace) {
	var index = "";
	var delim = ",";
	
	if(arrayLen(arguments) gte 4) delim = arguments[4];
	
	index = listFind(listToMatch, string, delim);
	
	if(index neq 0) return listGetAt(listToReplace, index, delim);
	else return string;
}
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