1
## Copyright (C) 2004, 2006, 2007 by Alois Schloegl
3
## This file is part of Octave.
5
## Octave is free software; you can redistribute it and/or modify it
6
## under the terms of the GNU General Public License as published by
7
## the Free Software Foundation; either version 3 of the License, or (at
8
## your option) any later version.
10
## Octave is distributed in the hope that it will be useful, but
11
## WITHOUT ANY WARRANTY; without even the implied warranty of
12
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
## General Public License for more details.
15
## You should have received a copy of the GNU General Public License
16
## along with Octave; see the file COPYING. If not, see
17
## <http://www.gnu.org/licenses/>.
20
## @deftypefn {Function File} {@var{idx} =} strfind (@var{str}, @var{pattern})
21
## @deftypefnx {Function File} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})
22
## Search for @var{pattern} in the string @var{str} and return the
23
## starting index of every such occurrence in the vector @var{idx}.
24
## If there is no such occurrence, or if @var{pattern} is longer
25
## than @var{str}, then @var{idx} is the empty array @code{[]}.
27
## If the cell array of strings @var{cellstr} is specified instead of the
28
## string @var{str}, then @var{idx} is a cell array of vectors, as specified
30
## @seealso{findstr, strmatch, strcmp, strncmp, strcmpi, strncmpi}
33
## Author: alois schloegl <a.schloegl@ieee.org>
34
## Created: 1 November 2004
35
## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
37
function idx = strfind (text, pattern)
41
elseif (! ischar (pattern))
42
error ("strfind: pattern must be a string value");
45
lp = length (pattern);
48
idx = __strfind_string__ (text, pattern, lp);
49
elseif (iscellstr (text))
50
idx = cell (size (text));
51
for i = 1:(numel (text))
52
idx{i} = __strfind_string__ (text{i}, pattern, lp);
55
error ("strfind: text must be a string or cell array of strings");
60
function idx = __strfind_string__ (text, pattern, lp)
62
idx = 1:(length (text) - lp + 1);
64
while (k < lp && ! isempty (idx))
65
idx = idx(text(idx + k) == pattern(++k));