Mercurial > octave-antonio
comparison scripts/strings/rindex.m @ 2271:707b9396ad2c
[project @ 1996-05-24 02:22:01 by jwe]
Initial revision
author | jwe |
---|---|
date | Fri, 24 May 1996 02:22:01 +0000 |
parents | |
children | 6dedd4e0a82f |
comparison
equal
deleted
inserted
replaced
2270:cfcf8ff7d2dd | 2271:707b9396ad2c |
---|---|
1 # Copyright (C) 1996 John W. Eaton | |
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 the | |
7 # Free Software Foundation; either version 2, or (at your option) any | |
8 # later version. | |
9 # | |
10 # Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 # 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 02111-1307, USA. | |
18 | |
19 function n = rindex (s, t) | |
20 | |
21 # usage: rindex (s, t) | |
22 # | |
23 # Returns the position of the last occurence of the string T in the | |
24 # string S or 0 if no occurence is found. | |
25 # | |
26 # NOTE: this function does not work for arrays of strings. | |
27 | |
28 # This is patterned after the AWK function of the same name. | |
29 | |
30 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. | |
31 | |
32 if (nargin != 2) | |
33 usage ("rindex (s, t)"); | |
34 endif | |
35 | |
36 n = 0; | |
37 | |
38 if (isstr (s) && isstr (t)) | |
39 | |
40 l_s = length (s); | |
41 l_t = length (t); | |
42 | |
43 if (l_t <= l_s) | |
44 tmp = l_s - l_t + 1; | |
45 for idx = tmp : -1 : 1 | |
46 if (strcmp (substr (s, idx, l_t), t)) | |
47 n = idx; | |
48 return; | |
49 endif | |
50 endfor | |
51 endif | |
52 | |
53 else | |
54 error ("rindex: expecting string arguments"); | |
55 endif | |
56 | |
57 endfunction |