2276
|
1 # Copyright (C) 1996 Kurt Hornik |
2271
|
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 = index (s, t) |
|
20 |
|
21 # usage: index (s, t) |
|
22 # |
|
23 # Returns the position of the first 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 ("index (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 = 1 : tmp |
|
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 ("index: expecting string arguments"); |
|
55 endif |
|
56 |
|
57 endfunction |