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

Searches for last occurence of a substring within a string.

Parameters

Name Type Description
text string Sub text to search for within text.

Returns

The last index of the occurence.

Example

Code example (#JS)

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

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


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

Output

The #JS code above produces the output shown below:

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

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