Loading

Use a 'contains' operator within an IF/ELSE AMPScript statement

Date de publication: Oct 13, 2022
Description

AMPScript does not have a native 'contains' feature for IF/ELSE statements, but it is possible to evaluate a string as TRUE if a certain value is 'contained' within by using AMPScript variables and the IndexOf() AMPScript function in your IF/ELSE statement.

 

Example

If [String] contains the word "Indianapolis," then return [This] value. 

Résolution

Example structure of the IndexOf() AMPScript function in an IF/ELSE statement

%%[ 
Var @String, @Value, @Output 

/* Set the value to check */ 
Set @Value= "Indianapolis" 

/* Set the string to check */ 
Set @String = "Welcome to Indianapolis, we hope you enjoy your stay!" 

/* Does it match; ; if no match, output of IndexOf(1,2) will be "0" */ 
if IndexOf(@String,@Value) > 0 then 
    Set @Output = "How was your stay in Indianapolis" 
else 
    Set @Output = "Where would you like to stay?" 
endif 

]%% 
<!-- Within the body of your email, you can print out the variable here -->
%%=v(@Output)=%%
 

Related Developer documentation

Numéro d’article de la base de connaissances

000386572

 
Chargement
Salesforce Help | Article