findstr

Finds the starting positions of the shorter text inside the longer one (legacy).

Category: Language Fundamentals > Data Types

Syntax

k = findstr(s1, s2)

Returns the start indices of occurrences of the shorter string within the longer one.

Description

findstr is kept for compatibility. Because it swaps its arguments silently, strfind(text, pattern) is clearer and is the recommended replacement.

Overlapping occurrences are all reported: searching 'ana' in 'banana' finds positions 2 and 4.

Input arguments

s1, s2
Character vectors. The order does not matter: the shorter is searched inside the longer.

Output arguments

k
Row vector of start positions, empty if there is no match.

Example

Overlapping matches

findstr('banana', 'ana')
findstr('abcabc', 'bc')
Output
ans = 1×2

     2     4
ans = 1×2

     2     5

See also

strfind contains strrep regexp extract