Skip to main content

index ()

Returns the first index at which a specified substring can be found in a string; otherwise, 0 is returned, indicating the substring is not present.

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
$start-at
The index at which to begin 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 first occurrence of the substring, or 0.

Example

@use '@sass-fairy/string';

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

@debug string.index($sentence, 'quick');
// 5

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

@debug string.index($sentence, 'dog', -4);
// 41

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