Need to check if one string contains another? Probably constantly. It's surprising that with how many string comparison operators and functions AMPscript has, there isn't a contains
operator or a Contains()
function.
Not to worry, you can do it with the IndexOf()
function.
Index Of What, Exactly?
The IndexOf()
function (SFMC documentation here) allows you to check where inside a larger string a smaller substring exists. For example:
%%=IndexOf('I am an AMPscript ninja.','ninja')=%%
This would result in 19
, because "ninja" starts at character 19 in the larger phrase "I am an AMPscript ninja."
If the larger string doesn't contain the substring at all, the result is 0
.
I don't know about you, but I've been AMPscripting for 8 years and never once had a use case where I needed to know where a character or set of characters occurred in a string. But I've been using IndexOf()
on a daily basis because...
Knowing WHERE Also Tells You IF
Quite simply, knowing WHERE the substring is located might not be useful very often, but knowing IF the substring is there makes this work as a 'contains' function. If the result is not 0
, then your larger string contains your smaller substring.
Here's another example, this time using variables:
%%[
set @string = 'I am an AMPscript ninja.'
set @substring = 'ninja'
if IndexOf(@string,@substring) != 0 then
set @string_contains_substring = true
endif
]%%
I see developers use != 0
(is not equal to zero) and > 0
(is greater than zero) pretty equally, and either will work.
Helpful Use Cases
If A Variable Contains A String
I find myself using this most checking if a variable contains a string. Let's look at a variable field with various superhero names, and see which ones contain "Man".
%%[if IndexOf(@superhero_name,'Man') != 0 then]%%
Hey, it's a classic superhero naming convention.
%%[else]%%
At least it's less repetitive.
%%[endif]%%
Batman, Catwoman, Iron Man, Spider-Man, and many others would be in the first if
statement. Any others not containing "Man" or "man" would be in the else
statement.
If A Variable Is On A List
Another way I like to use this function is to check if a variable's value is on a list of values. Instead of a long if
elseif
elseif
... endif
pattern, we can create a single variable with a list of values, then check to see if our current variable is on that list.
%%[
set @og_avengers = 'Iron Man,Captain America,Thor,Hulk,Hawkeye,Black Widow'
]%%
%%[if IndexOf(@og_avengers,@superhero_name) != 0 then]%%
No roster will ever be the same.
%%[else]%%
But Marvel/Disney will keep trying.
%%[endif]%%
My Favorite: Easy Geo-Targeting
For a more real-world example, this check-the-list concept gets very handy with geo-targeting.
%%[
set @snowbelt_states = 'AK,CO,CT,DE,IA,ID,IL,IN,KS,KY,MA,MD,ME,MI,MN,MO,MT,NC,ND,NE,NH,NJ,NY,OH,OR,PA,RI,SD,TN,VA,VT,WA,WI,WV,WY'
if IndexOf(@snowbelt_states,@state) != 0 or @country == 'CA' then
set @show_cold_weather_gear = true
else
set @show_cold_weather_gear = false
endif
]%%
If you want to get even narrower, pull all the postal codes in a radius around another postal code (my favorite map tool here), add those to a list, and show highly regionalized content for upcoming events.
I hope you found this helpful - if you have a favorite way to use IndexOf()
, leave a comment!
Top comments (0)