Replaces occurrences of substring1 in a string with obj, in a specified scope. The search is case-sensitive. Function returns original string with replacements made.
replace(string, substring1, obj [, scope])
returns string
string.replace(substring1, obj[, scope])
Name | Type | Required | Default | Description |
---|---|---|---|---|
string | string | Yes | String to search | |
substring1 | string | Yes | Substring to find within string | |
obj | any | Yes | String to replace substring1 with. As of CF2016+ you can also pass a callback function in this argument function(transform, position, original) . |
|
scope | string | No | one | * one: replace the first occurrence * all: replace all occurrences |
start | numeric | No | 1 | CF2021+ Position to start searching in the string. |
Replace in Script Syntax
getVal = replace("ColdFUsion", "U", "u");
writeDump(getVal);
Something similar in Tag Syntax
<cfset getVal1 = replace("COldFusiOn", "O", "o", "ONE") />
<cfdump var = #getVal1# />
In CF2016+ you can pass in a callback function to the third argument of the replace
function
public function upCase(pattern, position, orig) {
return uCase(pattern);
}
result = replace("A man a plan a canal.","an", upCase , "ALL");
writeOutput(result);
In CF2021+ you can pass position to start searching in the string
getRes = replace("Love ColdFusion", "o", "O","ALL","3");
writeOutput(getRes);