replace

Description

Replaces occurrences of substring1 in a string with substring2, in a specified scope. The search is case sensitive.

Categories

Related

Syntax

Replace(string, substring1, substring2 [, scope ])

Attributes

AttributeDescriptionRequiredDefault
scopeone: replaces the first occurrence (default) all: replaces all occurrences
stringA string or a variable that contains one. String in which to search.
substring1A string or a variable that contains one. String for which to search
substring2String that replaces substring1

Returns

The string, after making replacements.

Usage

To remove a string, specify the empty string ("") as substring2.
You do not need to escape comma characters in strings. For example, the following code deletes the commas from the sentence:
replace("The quick brown fox jumped over the lazy cow, dog, and cat.",",","","All")

Example

<h3>Replace Example</h3> 
 
The Replace function returns <I>string</I> with <I>substring1</I>  
    replaced by <I>substring2</I> in the specified scope. This  
    is a case-sensitive search. 
 
<cfif IsDefined("FORM.MyString")> 
Your original string, <cfoutput>#FORM.MyString#</cfoutput> 
You wanted to replace the substring <cfoutput>#FORM.MySubstring1# 
    </cfoutput> 
    with the substring <cfoutput>#FORM.MySubstring2#</cfoutput>. 
The result: <cfoutput>#Replace(FORM.myString,  
    FORM.MySubstring1, FORM.mySubString2)#</cfoutput> 
</cfif>