]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/rindex.m
update packages
[CreaPhase.git] / octave_packages / m / strings / rindex.m
1 ## Copyright (C) 1996-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} rindex (@var{s}, @var{t})
21 ## Return the position of the last occurrence of the character string
22 ## @var{t} in the character string @var{s}, or 0 if no occurrence is
23 ## found.  @var{s} may also be a string array or cell array of strings.
24 ##
25 ## For example:
26 ##
27 ## @example
28 ## @group
29 ## rindex ("Teststring", "t")
30 ##      @result{} 6
31 ## @end group
32 ## @end example
33 ##
34 ## The @code{rindex} function is equivalent to @code{index} with
35 ## @var{direction} set to @samp{"last"}.
36 ##
37 ## @seealso{find, index}
38 ## @end deftypefn
39
40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
41 ## Adapted-By: jwe
42 ## This is patterned after the AWK function of the same name.
43
44 function n = rindex (s, t)
45
46   if (nargin != 2)
47     print_usage ();
48   endif
49
50   n = index (s, t, "last");
51
52 endfunction
53
54
55 %!assert(rindex ("foobarbaz", "b") == 7 && rindex ("foobarbaz", "o") == 3);
56
57 %!test
58 %! str = char ("Hello", "World", "Goodbye", "World");
59 %! assert (rindex (str, "o"), [5; 2; 3; 2]);
60 %! str = cellstr (str);
61 %! assert (rindex (str, "o"), [5; 2; 3; 2]);
62
63 %% Test input validation
64 %!error rindex ()
65 %!error rindex ("foo")
66 %!error rindex ("foo", "bar", "last")
67