Main Content

strfind

Find strings within other strings

Description

example

k = strfind(str,pat) searches str for occurrences of pat. The output, k, indicates the starting index of each occurrence of pat in str. If pat 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,pat,'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 of substrings in a character vector.

First, create a character vector.

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

Find the substring in.

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

     2    15    19    36    41

There are five instances in str.

Find the substring In.

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

     []

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

Find the blank spaces in str.

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

     5     9    18    26    29    40    43    45    55

There are ten blank spaces in str.

Since R2020b

Create a character vector.

str = 'Find the letters.'
str = 
'Find the letters.'

Create a pattern that matches sequences of letters using the lettersPattern function.

pat = lettersPattern
pat = pattern
  Matching:

    lettersPattern

Find the index of each letter. While pat matches a sequence of letters having any length, strfind stops as soon as it finds a match and then proceeds to the next match. For example, 'Find' and 'F' are both matches for lettersPattern, since the number of letters for a match is not specified. But strfind matches 'F' first and returns its index. Then strfind matches 'i', and so on. (You can call lettersPattern with an optional argument that specifies the number of letters to match.)

k = strfind(str,pat)
k = 1×14

     1     2     3     4     6     7     8    10    11    12    13    14    15    16

To find the starts of words, call lettersPattern with boundaries. The letterBoundary function matches a boundary between letters and nonletter characters.

pat = letterBoundary + lettersPattern
pat = pattern
  Matching:

    letterBoundary + lettersPattern

k = strfind(str,pat)
k = 1×3

     1     6    10

For a list of functions that create pattern objects, see pattern.

Find the starting indices of substrings 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 wood in str.

idx = strfind(str,'wood')
idx=2×1 cell array
    {[10 23]}
    {[ 6 28]}

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

idx{:,:}
ans = 1×2

    10    23

ans = 1×2

     6    28

The substring 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 substring 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
    {[6 15]}

strfind returns a scalar cell that contains a numeric array, which contains indices of occurrences of the substring 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

Input text, specified as a string array, character vector, or cell array of character vectors.

Search pattern, specified as one of the following:

  • String scalar

  • Character vector

  • pattern scalar (since R2020b)

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

Output Arguments

collapse all

Indices of occurrences of pat, returned as an array. If pat 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 pat.

  • 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 pat.

Tips

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

  • The contains function is recommended for finding patterns within string arrays.

Extended Capabilities

Version History

Introduced before R2006a