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.
IndexOf() function has two parameters:
sourceString(string): Required. The string to analyze.
substring(string): Required. The character or substring to find the position of.
To use the function, pass it a source string, and the substring that you want to find in that string.
This example finds a single character within a string and returns its position. It uses simple conditional logic to determine which text to output.
The example outputs the result of the function.
This example finds a substring that consists of multiple consecutive characters.
The function returns the position at which the substring begins.