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, strfind returns a vector of type double.

  • If str is a cell array of character vectors, 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 Pattern in Character Vector

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 Pattern in Cell Array

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 = 

    [1x2 double]
    [1x2 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.

Return Indices in Cell Array

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 = 

    [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 =

     6    15

Input Arguments

collapse all

str — Data to be searchedcharacter vector | cell array of character vectors

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

Data Types: char | cell

pattern — Search patterncharacter vector

Search pattern, specified as a character vector.

Data Types: char

cellOutput — Indicator for forcing output to be returned as cell arrayfalse (default) | true | 0 | 1

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

k — Indices of occurrences of patternarray

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, k is a vector of doubles indicating the index of each occurrence of pattern.

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

More About

collapse all

Tips

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

Introduced before R2006a

Was this topic helpful?