Skip to main content

last-index ()

Returns the last index at which a specified substring can be found in a string; otherwise, 0 is returned, indicating the substring is not present. The string is searched forwards, ending at a given index when specified.

Parameters

$string
The source string in which the substring is to be located.
Type
String
$substring
The substring to locate in the source string.
Type
String
$end-at
The index at which to end the search. A negative index can be used, indicating an offset from the end of the source string.
Type
Number
Default
1

Return Value

Number
The index of the last occurrence of the substring, or 0.

Example

@use '@sass-fairy/string';

$sentence: 'The quick brown fox jumps over the lazy dog.';

@debug string.last-index($sentence, 'he');
// 33

@debug string.last-index($sentence, 'cat');
// 0