| MATLAB Function Reference | ![]() |
Find one string within another
Syntax
Description
k = strfind(str, pattern)
searches the string str for occurrences of a shorter string, pattern, and returns the starting index of each such occurrence in the double array k. If pattern is not found in str, or if pattern is longer than str, then strfind returns the empty array [].
k = strfind(cellstr, pattern)
searches each string in cell array of strings cellstr for occurrences of a shorter string, pattern, and returns the starting index of each such occurrence in cell array k. If pattern is not found in a string or if pattern is longer then all strings in the cell array, then strfind returns the empty array [], for that string in the cell array.
The search performed by strfind is case sensitive. Any leading and trailing blanks in pattern or in the strings being searched are explicitly included in the comparison.
Examples
Use strfind to find a two-letter pattern in string S:
S = 'Find the starting indices of the pattern string'; strfind(S, 'in') ans = 2 15 19 45 strfind(S, 'In') ans = [] strfind(S, ' ') ans = 5 9 18 26 29 33 41
Use strfind on a cell array of strings:
cstr = {'How much wood would a woodchuck chuck'; 'if a woodchuck could chuck wood?'}; idx = strfind(cstr, 'wood'); idx{:,:} ans = 10 23 ans = 6 28
This means that 'wood' occurs at indices 10 and 23 in the first string and at indices 6 and 28 in the second.
See Also
findstr, strmatch, strtok, strcmp, strncmp, strcmpi, strncmpi, regexp, regexpi, regexprep
| streamtube | strings | ![]() |
© 1994-2005 The MathWorks, Inc.