Overview

As we dive into more complex AMPscript logic we may come across situations where we need to manipulate string values to achieve different things. We can do this by using the following functions listed below:

Replace(1, 2, 3)
Length(1)
IndexOf(1, 2)
Substring(1, 2, 3)
Concat(1, 2)


Examples

Replace Function
There may be instances where a client is using dynamic email copy to deliver highly personalized emails to its users. The client realizes they did not update their data and accidently left references to 2007 in their dynamic email copy. To save the client time, we can dynamically convert 2007 to 2008 using the Replace function.

<!-- Example Code -->
%%[
/**
* Replace Logic
* Replaces 2007 with 2008 from the original string
*/
VAR @value1, @value2
SET @value1 = "The 2007 model is better"
SET @value2 = Replace(@value1,"2007","2008")
]%%
%%=v(@value2)=%%
<!-- Example Output -->
The 2008 model is better
View this gist on GitHub

Length Function
There may be an instance where the client wants to show the last four characters of a subscriber’s credit card number. In this situation, we would use the Length function to determine how many characters a subscriber’s credit card number is. Then you’d use Length in conjunction with the Substring function to show the last four digits.

<!-- Example Code -->
%%[
/**
* Length Logic
* The length function returns a number value
*/
VAR @value1, @value2
SET @value1 = "Hello World!"
SET @value2 = Length(@value1)
]%%
%%=v(@value2)=%%
<!-- Example Output -->
12
View this gist on GitHub

Substring Function
Returns the portion of the specified string starting with the specified character position and no longer than the specified length. There may be an instance where the client wants you to show a portion of the string value. In this case, you’d want to use Substring to handle that requirement.

<!-- Example Code -->
%%[
/**
* Substring Logic
* Only show two characters starting with the second character position
*/
VAR @value1, @value2
SET @value1 = "ABCDEF"
SET @value2 = Substring(@value1,2,2)
]%%
%%=v(@value2)=%%
<!-- Example Output -->
BC
View this gist on GitHub

Concat Function
Concatenates the strings specified in the arguments. Include as many values as necessary. It’s common for clients to ask us to append certain tracking variables to the end of a click thru URL. We can use the Concat function to simply attach query string variables to links.

<!-- Example Code -->
%%[
/*
* Concat Logic
* This combines three string values into a single string to show
*/
VAR @value1, @value2, @value3
SET @value1 = "This "
SET @value2 = "is "
SET @value3 = "cool"
SET @final = Concat(@value1,@value2,@value3)
]%%
%%=v(@final)=%%
<!-- Example Output -->
This is cool.
View this gist on GitHub

IndexOf Function
Sometimes clients ask us to hide and show email content based off keywords in their dynamic data. For example, the client sends a monthly newsletter to their subscribers. They want us to show a special coupon for premium users. Premium users are identified when the word premium is used in the cust_status field. The client wants you to show this special coupon when cust_status contains the word premium.

<!-- Example Code -->
%%[
/**
* IndexOf Logic
* Checks to see if the word Premium exists in the string value.
*/
VAR @cust_status, @cust_coupon
SET @cust_status = "Premium Retail Customer"
IF IndexOf(@cust_status,"Premium") > 0 THEN
SET @cust_coupon = "Show Premium Coupon"
ELSE
SET @cust_coupon = "Show Standard Coupon"
ENDIF
]%%
%%=v(@cust_coupon)=%%
<!-- Example Output -->
Show Premium Coupon
View this gist on GitHub

Gallery