1######################################################################## 2## 3## Copyright (C) 1996-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} rindex (@var{s}, @var{t}) 28## Return the position of the last occurrence of the character string 29## @var{t} in the character string @var{s}, or 0 if no occurrence is 30## found. 31## 32## @var{s} may also be a string array or cell array of strings. 33## 34## For example: 35## 36## @example 37## @group 38## rindex ("Teststring", "t") 39## @result{} 6 40## @end group 41## @end example 42## 43## The @code{rindex} function is equivalent to @code{index} with 44## @var{direction} set to @qcode{"last"}. 45## 46## @seealso{find, index} 47## @end deftypefn 48 49function n = rindex (s, t) 50 51 if (nargin != 2) 52 print_usage (); 53 endif 54 55 n = index (s, t, "last"); 56 57endfunction 58 59 60%!assert (rindex ("foobarbaz", "b"), 7) 61%!assert (rindex ("foobarbaz", "o"), 3) 62 63%!test 64%! str = char ("Hello", "World", "Goodbye", "World"); 65%! assert (rindex (str, "o"), [5; 2; 3; 2]); 66%! str = cellstr (str); 67%! assert (rindex (str, "o"), [5; 2; 3; 2]); 68 69## Test input validation 70%!error rindex () 71%!error rindex ("foo") 72%!error rindex ("foo", "bar", "last") 73