The Replace function is a way of replacing characters in a string — similar to the ReplaceList function but applies to one string value. It allows you to search for string values and replace them with another string value. A good use case is to replace symbols with the ASCII code to escape it.
Syntax:
Replace (1,2,3)
1 = String value that you want to search and replace values in (can be a variable)
2 = Replacement string
3 = String used as replacement
Example:
%%[
var @emailaddress
set @emailaddress = “ampscript+test@ampscript.com”
]%%
%%=Replace(@emailaddress, ‘+’, ‘+’)=%%
Output:
ampscript+test@ampscript.com
Explanation:
In the AMPscript above, first we are declaring the variables using the var
function by using the email address. Then defining the @emailaddress
variable with the string value for the email address we are using in this example.
We want to Replace
function to escape out the + symbol as this can cause issues, i.e. when passing that value through a URL: “ampscript+test@ampscript.com”. The Replace
function looks for the + symbol and replaces it with “+” which is the ASCII version of the + symbol.
The output of that value is “ampscript+test@ampscript.com” and renders correctly with the plus when outputted on to a Cloud page, for example.