strfind

Find strings within other strings

Description

example

k = strfind(str,pattern) searches str for occurrences of pattern. The output, k, indicates the starting index of each occurrence of pattern in str. If pattern is not found, then strfind returns an empty array, []. The strfind function executes a case-sensitive search.

  • If str is a character vector or a string scalar, then strfind returns a vector of type double.

  • If str is a cell array of character vectors or a string array, then strfind returns a cell array of vectors of type double.

example

k = strfind(str,pattern,'ForceCellOutput',cellOutput) forces strfind to return k as a cell array when cellOutput is true, even when str is a character vector.

Examples

collapse all

Find the starting indices for occurrences of patterns in a character vector.

First, create a character vector.

str = 'Find the starting indices of a pattern in a character vector';

Find the pattern in.

k = strfind(str,'in')
k = 1×4

     2    15    19    40

There are four instances of the pattern in str.

Find the pattern In.

k = strfind(str,'In')
k =

     []

Since strfind is case sensitive, the pattern is not found. k is an empty array.

Find the blank spaces in str.

k = strfind(str,' ')
k = 1×10

     5     9    18    26    29    31    39    42    44    54

There are ten blank spaces in str.

Find the starting indices for occurrences of a pattern in a cell array of character vectors.

Create a cell array of character vectors.

str = {'How much wood would a woodchuck chuck';
       'if a woodchuck could chuck wood?'};

Find the pattern wood in str.

idx = strfind(str,'wood')
idx=2×1 cell array
    {1x2 double}
    {1x2 double}

Examine the output cell array to find the instances of the pattern.

idx{:,:}
ans = 1×2

    10    23

ans = 1×2

     6    28

The pattern wood occurs at indices 10 and 23 in the first character vector and at indices 6 and 28 in the second character vector.

Find the occurrences of a pattern in a character vector. Force strfind to return the indices of those occurrences in a cell array. Then display the indices.

Create a character vector and find the occurrences of the pattern ain.

str = 'The rain in Spain.';
k = strfind(str,'ain','ForceCellOutput',true)
k = 1x1 cell array
    {1x2 double}

strfind returns a scalar cell that contains a numeric array, which contains indices of occurrences of the pattern ain in str. To access the numeric array within the cell, use curly braces.

k{1}
ans = 1×2

     6    15

Input Arguments

collapse all

Data to be searched, specified as a character vector, a cell array of character vectors, or a string array.

Data Types: char | cell | string

Search pattern, specified as a character vector or a string scalar.

Data Types: char | string

Indicator for forcing output to be returned as a cell array, specified as false, true, 0, or 1.

Data Types: logical

Output Arguments

collapse all

Indices of occurrences of pattern, returned as an array. If pattern is not found, then k is an empty array, [].

  • If str is a character vector or a string scalar, k is a vector of doubles indicating the index of each occurrence of pattern.

  • If str is a cell array of character vectors or a string array, k is a cell array. For each piece of text in str, the corresponding cell of k contains a vector of doubles indicating the index of each occurrence of pattern.

Tips

  • If pattern is a character vector or string scalar with no characters ('' or ""), then strfind returns an empty array.

  • Starting in R2016b, the contains function is recommended for finding patterns within string arrays.

Extended Capabilities

Introduced before R2006a