AMPscript IndexOf (Next)
Find the 1-based position of a substring with AMPscript IndexOf in Marketing Cloud Next. Syntax, examples, and behaviour proven in a Marketing Cloud Next email, including the third argument Next rejects.
Syntax
IndexOf(sourceString, substring) → number
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sourceString |
string | Yes | String to search in |
substring |
string | Yes | Substring to find |
Exactly two arguments. Pass text: a number or boolean in either position makes the email fail to render.
Example
%%=IndexOf("Hello World", "World")=%%
Renders 7.
The search ignores letter case:
%%=IndexOf("Hello World", "WORLD")=%%
Also renders 7.
Return value
number — the 1-based position where the substring starts.
0 means the substring was not found, or the source string is empty. Note that an empty substring in a non-empty source gives 1, not 0.
Behaviour
Positions start at 1. In Hello World, H is at 1 and the final d at 11. A substring equal to the whole source gives 1.
The match ignores case. "WORLD", "world" and "World" all give 7 in Hello World, and "h" gives 1.
Only the first match counts. IndexOf("banana", "na") gives 3, although na occurs again at 5.
Not found gives 0. IndexOf("Hello World", "zzz") and a substring longer than the source (IndexOf("ab", "abcdef")) both give 0.
Empty values. An empty source gives 0 (IndexOf("", "a") and IndexOf("", "")), but an empty substring in a non-empty source gives 1 (IndexOf("Hello", "")). Check for an empty substring yourself if 1 would be read as a match.
Positions count UTF-16 code units, like Length. In Concat("caf", Char(233), " time"), which measures 9, the é is at 4 and time at 6.
There is no third argument. Every email with a third argument was rejected at save with HTTP 400 “The AMPscript syntax isn’t valid”: 1, 2, 3, 0, -1, -9, the string "2", a decimal, a boolean and "abc". To find a later occurrence, search the rest of the string after the first match with Substring.
Numbers and booleans break the preview. IndexOf(9876543, "65"), IndexOf("a1b2c3", 2), IndexOf(true, "ru") and IndexOf("is true here", true) each save, but the email does not render (HTTP 500).
Too few or too many arguments cannot be saved. IndexOf(), IndexOf("Hello World") and a four-argument call are rejected at save.
Show Next test script
<!-- Marketing Cloud Next test script for IndexOf. Paste it into the HTML component of a
Next email and open the preview. Each line prints the result Next gave when the call
was rendered on its own (expect) next to the result of this run (got).
Assembled from single-call test emails (dates on the page); it has not been
run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[7] got=[%%=IndexOf("Hello World","World")=%%]</p>
<p>c2 expect=[3] got=[%%=IndexOf("banana","na")=%%]</p>
<p>c3 expect=[7] got=[%%=IndexOf("Hello World","WORLD")=%%]</p>
<p>c4 expect=[1] got=[%%=IndexOf("Hello World", "H")=%%]</p>
<p>c5 expect=[11] got=[%%=IndexOf("Hello World", "d")=%%]</p>
<p>c6 expect=[1] got=[%%=IndexOf("Hello World", "Hello World")=%%]</p>
<p>c7 expect=[0] got=[%%=IndexOf("Hello World", "zzz")=%%]</p>
<p>c8 expect=[0] got=[%%=IndexOf("ab", "abcdef")=%%]</p>
<p>c9 expect=[7] got=[%%=IndexOf("Hello World", "world")=%%]</p>
<p>c10 expect=[1] got=[%%=IndexOf("Hello World", "h")=%%]</p>
<p>c11 expect=[1] got=[%%=IndexOf("Hello", "")=%%]</p>
<p>c12 expect=[0] got=[%%=IndexOf("", "a")=%%]</p>
<p>c13 expect=[0] got=[%%=IndexOf("", "")=%%]</p>
<p>c14 expect=[9] got=[%%=Length(Concat("caf",Char(233)," time"))=%%]</p>
<p>c15 expect=[4] got=[%%=IndexOf(Concat("caf",Char(233)," time"), Char(233))=%%]</p>
<p>c16 expect=[6] got=[%%=IndexOf(Concat("caf",Char(233)," time"), "time")=%%]</p>
Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
would stop the whole preview or block the save. -->
<!-- email 1: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World","o",2)=%%
<!-- email 2: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World", "o", 1)=%%
<!-- email 3: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World", "o", 3)=%%
<!-- email 4: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World", "l", 3)=%%
<!-- email 5: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("banana", "na", 2)=%%
<!-- email 6: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("aaaa", "aa", 2)=%%
<!-- email 7: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World", "o", 0)=%%
<!-- email 8: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("banana", "na", -1)=%%
<!-- email 9: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("abcabcabc", "abc", -1)=%%
<!-- email 10: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("abcabcabc", "abc", -9)=%%
<!-- email 11: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("banana", "na", "2")=%%
<!-- email 12: expect that the email saves, but the preview fails (HTTP 500) -->
%%=IndexOf(9876543, "65")=%%
<!-- email 13: expect that the email saves, but the preview fails (HTTP 500) -->
%%=IndexOf("a1b2c3", 2)=%%
<!-- email 14: expect that the email saves, but the preview fails (HTTP 500) -->
%%=IndexOf(true, "ru")=%%
<!-- email 15: expect that the email saves, but the preview fails (HTTP 500) -->
%%=IndexOf("is true here", true)=%%
<!-- email 16: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("banana", "na", 1.5)=%%
<!-- email 17: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("banana", "na", true)=%%
<!-- email 18: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World", "o", "abc")=%%
<!-- email 19: expect that the save is rejected (HTTP 400) -->
%%=IndexOf()=%%
<!-- email 20: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World")=%%
<!-- email 21: expect that the save is rejected (HTTP 400) -->
%%=IndexOf("Hello World", "o", 1, 2)=%%
Differs from official docs
- Matching ignores case. The reference does not say whether case matters. In Next,
IndexOf("Hello World", "WORLD")gives7andIndexOf("Hello World", "h")gives1. Card - An empty search value counts as found. The reference does not cover it; Next returns
1forIndexOf("Hello", ""). Card - Numbers and booleans break the render. The reference does not rule them out, but
IndexOf(9876543, "65")andIndexOf("a1b2c3", 2)made the Next preview fail with HTTP 500. Card
Availability & differences
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | Yes, from API 67.0 |
Engagement accepts a third argument that picks the occurrence (IndexOf("Hello World","o",2) gives 8); Next rejects any third argument at save. An empty substring gives 0 in Engagement but 1 in Next, and a number or boolean argument, which Engagement searches as text, makes the Next preview fail. Compared 2026-11-23 by rendering the same calls in a Marketing Cloud Next email.
See also
Length— the counting unit the returned positions useSubstring— cut the text at the position you foundConcat— builds the strings you search- Official reference