Returns the position at which a substring occurs within a string. The function uses 1-based indexing—that is, the first character in the string is counted as position 1. A result of 0 indicates that the substring wasn’t found in the source string.
Availability
<table> <tr> <td>Marketing Cloud Engagement</td> <td>✅ Yes</td> </tr> <tr> <td>Marketing Cloud Next</td> <td>✅ Yes</td> </tr> </table>This function became available in Marketing Cloud Next during the Summer ’26 release (API version 67.0).
Syntax
IndexOf(sourceString, substring)
The IndexOf() function has two parameters:
sourceString(string): Required. The string to analyze.substring(string): Required. The character or substring to find the position of.
Usage
To use the function, pass it a source string, and the substring that you want to find in that string.
Finding a Single Character
This example finds a single character within a string and returns its position. It uses simple conditional logic to determine which text to output.
%%[
Var @tickedId, @isTicketClassC
Set @ticketId = "29487C-22934"
Set @isTicketClassC = IndexOf(@ticketId, "C")
If @isTicketClassC == 6 then
]%%
<p>Your ticket is Class C</p>
%%[ Else ]%%
<p>Your ticket is not Class C</p>
%%[ EndIf ]%%
The example outputs the result of the function.
Your ticket is Class C
Finding a Substring with Multiple Characters
This example finds a substring that consists of multiple consecutive characters.
%%[
Var @lipsum, @position, @substringToFind
Set @lipsum = "Lorem ipsum dolor sit amet."
Set @substringToFind = "dolor"
Set @position = IndexOf(@lipsum, @substringToFind)
]%%
<p>Found the string "%%=v(@substringToFind)=%%" at position %%=v(@position)=%%.</p>
The function returns the position at which the substring begins.
Found the string "dolor" at position 13.