Skip to content

index

strings · scripts/strings/index.m

: n = index (s, t)
: n = index (s, t, direction)

Locate the substring t inside s.

The result is the one-based position at which t starts in s, or 0 when t does not occur. By default the first occurrence is reported; with direction set to "last" the last one is reported instead ("first" is the default and may be given explicitly).

s may be a character matrix or a cell array of strings, in which case one position is returned per row or per cell.

index ("banana", "an")
    ⇒  2
index ("banana", "an", "last")
    ⇒  4