CFLib.org – Common Function Library Project

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

Last updated August 04, 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;
}

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