home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / strings / rindex.m < prev    next >
Text File  |  1999-12-24  |  2KB  |  66 lines

  1. ## Copyright (C) 1996 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 2, or (at your option)
  8. ## 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, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} rindex (@var{s}, @var{t})
  22. ## Return the position of the last occurrence of the string @var{t} in the
  23. ## string @var{s}, or 0 if no occurrence is found.  For example,
  24. ## 
  25. ## @example
  26. ## rindex ("Teststring", "t")
  27. ##      @result{} 6
  28. ## @end example
  29. ## 
  30. ## @strong{Note:}  This function does not work for arrays of strings.
  31. ## @end deftypefn
  32.  
  33. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  34. ## Adapted-By: jwe
  35.  
  36. function n = rindex (s, t)
  37.  
  38.   ## This is patterned after the AWK function of the same name.
  39.  
  40.   if (nargin != 2)
  41.     usage ("rindex (s, t)");
  42.   endif
  43.  
  44.   n = 0;
  45.  
  46.   if (isstr (s) && isstr (t))
  47.  
  48.     l_s = length (s);
  49.     l_t = length (t);
  50.  
  51.     if (l_t <= l_s)
  52.       tmp = l_s - l_t + 1;
  53.       for idx = tmp : -1 : 1
  54.     if (strcmp (substr (s, idx, l_t), t))
  55.       n = idx;
  56.       return;
  57.     endif
  58.       endfor
  59.     endif
  60.  
  61.   else
  62.     error ("rindex: expecting string arguments");
  63.   endif
  64.  
  65. endfunction
  66.