Find strings within other strings
collapse all in page
Syntax
k = strfind(str,pat)
k = strfind(str,pat,'ForceCellOutput',cellOutput)
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 vectoror a string scalar, thenstrfind
returns a vectorof typedouble
.If
str
is a cell array of charactervectors or a string array, thenstrfind
returnsa cell array of vectors of typedouble
.
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 Substrings in Character Vector
Open Live Script
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
.
Find Letters and Words Using Patterns
Open Live Script
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 Substrings in Cell Array
Open Live Script
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.
Return Indices in Cell Array
Open Live Script
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
str
— Input text
string array | character vector | cell array of character vectors
Input text, specified as a string array, character vector, or cell array of character vectors.
pat
— Search pattern
string scalar | character vector | pattern
scalar (since R2020b)
Search pattern, specified as one of the following:
String scalar
Character vector
pattern scalar (since R2020b)
cellOutput
— Indicator for forcing output to be returned as cell array
false
(default) | true
| 0
| 1
Indicator for forcing output to be returned as a cell array,specified as false
, true
, 0
,or 1
.
Output Arguments
collapse all
k
— Indices of occurrences of pat
array
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 ofpat
.If
str
is a cell array of character vectors or a string array,k
is a cell array. For each piece of text instr
, the corresponding cell ofk
contains a vector of doubles indicating the index of each occurrence ofpat
.
Tips
If
pat
is a character vector or string scalar with no characters (''
or""
), thenstrfind
returns an empty array.The
contains
function is recommended for finding patterns within string arrays.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
The text input must be a tall array of strings or a tall cell array of character vectors.
The pattern input must be a single string, and it must not be a tall array or a pattern object.
The output is a tall cell array of index vectors, with one element per input string.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
must be a string scalar or a character vector.Generated code returns an empty output as a 1-by-0character array.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
str
must be a string array or a cell array of character vectors.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
count | replace | strtok | strcmp | regexp | split | contains | pattern | startsWith | endsWith | matches | extract
Topics
- Create String Arrays
- Search and Replace Text
- Build Pattern Expressions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office