1 ## Copyright (C) 1996-2012 Kurt Hornik
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} {} index (@var{s}, @var{t})
21 ## @deftypefnx {Function File} {} index (@var{s}, @var{t}, @var{direction})
22 ## Return the position of the first occurrence of the string @var{t} in the
23 ## string @var{s}, or 0 if no occurrence is found. @var{s} may also be a
24 ## string array or cell array of strings.
30 ## index ("Teststring", "t")
35 ## If @var{direction} is @samp{"first"}, return the first element found.
36 ## If @var{direction} is @samp{"last"}, return the last element found.
38 ## @seealso{find, rindex}
41 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
43 ## This is patterned after the AWK function of the same name.
45 function n = index (s, t, direction = "first")
47 if (nargin < 2 || nargin > 3)
53 s = cellstr (s); # Handle string arrays by conversion to cellstr
55 elseif (! iscellstr (s))
56 error ("index: S must be a string, string array, or cellstr");
63 f(cellfun ("isempty", f)) = {0};
66 direction = tolower (direction);
68 if (strcmp (direction, "first"))
70 n = cellfun ("min", f);
74 elseif (strcmp (direction, "last"))
76 n = cellfun ("max", f);
81 error ('index: DIRECTION must be either "first" or "last"');
87 %!assert (index ("foobarbaz", "b") == 4 && index ("foobarbaz", "z") == 9);
89 %!assert (index("astringbstringcstring", "s"), 2)
90 %!assert (index("astringbstringcstring", "st"), 2)
91 %!assert (index("astringbstringcstring", "str"), 2)
92 %!assert (index("astringbstringcstring", "string"), 2)
93 %!assert (index("abc---", "abc+++"), 0)
95 ## test everything out in reverse
96 %!assert (index("astringbstringcstring", "s", "last"), 16)
97 %!assert (index("astringbstringcstring", "st", "last"), 16)
98 %!assert (index("astringbstringcstring", "str", "last"), 16)
99 %!assert (index("astringbstringcstring", "string", "last"), 16)
100 %!assert (index("abc---", "abc+++", "last"), 0)
103 %! str = char ("Hello", "World", "Goodbye", "World");
104 %! assert (index (str, "o"), [5; 2; 2; 2]);
105 %! assert (index (str, "o", "last"), [5; 2; 3; 2]);
106 %! str = cellstr (str);
107 %! assert (index (str, "o"), [5; 2; 2; 2]);
108 %! assert (index (str, "o", "last"), [5; 2; 3; 2]);
110 %% Test input validation
113 %!error index ("a", "b", "first", "d")
114 %!error index (1, "bar")
115 %!error index ("foo", "bar", 3)