2313
|
1 ## Copyright (C) 1996 Kurt Hornik |
2325
|
2 ## |
2313
|
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. |
2271
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} index (@var{s}, @var{t}) |
|
22 ## Return the position of the first occurrence of the string @var{t} in the |
|
23 ## string @var{s}, or 0 if no occurrence is found. For example, |
3426
|
24 ## |
3361
|
25 ## @example |
|
26 ## index ("Teststring", "t") |
|
27 ## @result{} 4 |
|
28 ## @end example |
3426
|
29 ## |
3361
|
30 ## @strong{Note:} This function does not work for arrays of strings. |
|
31 ## @end deftypefn |
2271
|
32 |
2355
|
33 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
34 ## Adapted-By: jwe |
2314
|
35 |
2311
|
36 function n = index (s, t) |
2271
|
37 |
2303
|
38 ## This is patterned after the AWK function of the same name. |
2271
|
39 |
|
40 if (nargin != 2) |
|
41 usage ("index (s, t)"); |
|
42 endif |
|
43 |
|
44 n = 0; |
|
45 |
|
46 if (isstr (s) && isstr (t)) |
|
47 |
3240
|
48 [nr_s, l_s] = size (s); |
|
49 [nr_t, l_t] = size (t); |
|
50 |
3759
|
51 if (nr_s == 0 || nr_t == 0) |
|
52 return; |
|
53 endif |
|
54 |
3240
|
55 if (nr_s != 1 || nr_t != 1) |
|
56 error ("index: arguments cannot be string arrays"); |
|
57 endif |
2271
|
58 |
|
59 if (l_t <= l_s) |
|
60 tmp = l_s - l_t + 1; |
|
61 for idx = 1 : tmp |
3426
|
62 if (strcmp (substr (s, idx, l_t), t)) |
|
63 n = idx; |
|
64 return; |
|
65 endif |
2271
|
66 endfor |
|
67 endif |
|
68 |
|
69 else |
|
70 error ("index: expecting string arguments"); |
|
71 endif |
2325
|
72 |
2271
|
73 endfunction |