Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

strfind

Find one string within another

Syntax

  • k = strfind(str,pattern)
    example
  • k = strfind(str,pattern,'ForceCellOutput',cellOutput)
    example

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 =

     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 =

     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

    [1×2 double]
    [1×2 double]

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

idx{:,:}
ans =

    10    23


ans =

     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 =

  cell

    [1×2 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 =

     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.

More About

collapse all

Tall Array Support

This function supports tall arrays with the limitations:

  • The text input must be a tall array of strings or a tall cell array of character vectors.

  • The text pattern must be a non-tall single string.

  • The output is a cell array of index vectors, with one element per input string.

For more information, see Tall Arrays.

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.

Introduced before R2006a

Was this topic helpful?