Print this page

Use a "contains" operator in an IF/ELSE AMPScript statement

Knowledge Article Number 000231525
When using AMPScript IF/ELSE statements, you may come across a need to evaluate a string as TRUE if a certain value is "contained" within it. This will give you greater flexibility when evaluating certain strings and allow you to avoid just using an "equal to" operator.  

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

With the use of AMPScript variables and the IndexOf() AMPScript function, you can achieve this functionality.  

In order to achieve this, you can utilize the IndexOf() AMPScript function in your IF/ELSE statement.  Here's the structure: 
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 IndexOf(@String,@Value) > 0 then 
    Set @Output = "How was your stay in Indianapolis" 
    Set @Output = "Where would you like to stay?" 

<!-- Within the body of your email, you can print out the variable here -->


promote demote