CFLib.org – Common Function Library Project

replaceStructNoCase(argString, argStruct[, argStartSymbol][, argEndSymbol])

Last updated May 30, 2011

author

Rodion Bykov

Version: 1 | Requires: CF6 | Library: StrLib

Description:
Useful whe you need to replace many sub-strings in a string at once, and have structure which key names are exactly same as sub-strings to be replaced.

Return Values:
Returns a string.

Example:

<cfset stringToReplace = "Dear {CLIENT}, we are happy to deliver your Order ##{ORDER}" />

<cfset structToReplace = structNew() />
<cfset structToReplace.client = "John Smith" />
<cfset structToReplace.order = "15" />

<cfset result = replaceStructNoCase(stringToReplace, structToReplace) />

Parameters:

Name Description Required
argString String to parse. Yes
argStruct Structure to use for values. Yes
argStartSymbol Symbol used to denote beginning of token. Defaults to { No
argEndSymbol Symbol used to denote end of token. Defaults to }. No

Full UDF Source:

<!---
 Returns string, with occurence of structure key names replaced by structure values.
 
 @param argString      String to parse. (Required)
 @param argStruct      Structure to use for values. (Required)
 @param argStartSymbol      Symbol used to denote beginning of token. Defaults to { (Optional)
 @param argEndSymbol      Symbol used to denote end of token. Defaults to }. (Optional)
 @return Returns a string. 
 @author Rodion Bykov (rodionbykov@rodionbykov.com) 
 @version 1, May 30, 2011 
--->
<cffunction name="replaceStructNoCase" returntype="string">
    <cfargument name="argString" type="string" required="true" />
    <cfargument name="argStruct" type="struct" required="true" />
    <cfargument name="argStartSymbol" type="string" required="false" default="{" />
    <cfargument name="argEndSymbol" type="string" required="false" default="}" />
    
    <cfset var result = "" />
    <cfset var i = "" />
    
    <cfset result = arguments.argString />
    
    <cfloop collection="#arguments.argStruct#" item="i">
        <cfset result = replaceNoCase( result, arguments.argStartSymbol & i & arguments.argEndSymbol, StructFind(arguments.argStruct, i), "all" ) />
    </cfloop>
    
    <cfreturn result />
</cffunction>

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