Description

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

This function can take either string or callback function as an argument instead of the argument to replace the string.

Returns

The string, after making replacements.

Category

String functions

Function syntax

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

See also

FindREFindReplaceNoCaseReplaceListREReplace

Parameters

Parameter

Description

string

A string or a variable that contains one. String in which to search.

substring1

A string or a variable that contains one. String for which to search

obj

String that replaces substring1 or callback function. You can also pass a callback function in this argument.

function(transform,position,original) {...}

scope

  • one: replaces the first occurrence (default)
  • all: replaces all occurrences

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

<cfoutput>#replace("The quick brown fox","o","cf","all")#</cfoutput>

The code produces the following output:

The quick brcfwn fcfx

Example with callback function

<cfscript>
       myStr="happy app application apply appreciate appreciation Apprentice";
       
       outStr = replace( myStr, "app", function (transform, position, original){
                    return UCase(transform);
             }, "all");
       writeoutput(outStr);
</cfscript>

The code produces the following output:

hAPPy APP APPlication APPly APPreciate APPreciation APPrentice

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy