String.indexOf(text) Last updated: 13. Nov 2023

Searches the entire calling string, and returns the index of the first occurrence of the specified substring.

Parameters

Name Type Description
text string Text to search for.

Returns

Returns the index of the first occurence, if not found it returns -1.

Example

Code example (#JS)

#JS is mixed HTML (or other text file) with inline JavaScript with # starting and ending each inline statement.
#{
    var text = "This is a test";
    var search = "test";
    var notfound = "xyz";
}#

<b>Found in string:</b>
#{
    var index = text.indexOf(search);
    write(index);
}#


<b>Not found in string:</b>
#{
    index = text.indexOf(notfound);
    write(index);    
}#

Output

The #JS code above produces the output shown below:

<b>Found in string:</b>
10

<b>Not found in string:</b>
-1