comparison scripts/strings/findstr.m @ 2272:e97fba45f0a3

[project @ 1996-05-24 02:40:06 by jwe] Initial revision
author jwe
date Fri, 24 May 1996 02:40:06 +0000
parents
children 38fea6d34daf
comparison
equal deleted inserted replaced
2271:707b9396ad2c 2272:e97fba45f0a3
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 v = findstr (s, t)
20
21 # usage: findstr (s, t)
22 #
23 # Returns the vector of all positions in the longer of the two strings
24 # S and T where an occurence of the shorter of the two starts.
25
26 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>.
27
28 if (nargin != 2)
29 usage ("findstr (s, t)");
30 endif
31
32 if (isstr (s) && isstr (t))
33
34 # Make S be the longer string.
35
36 if (length (s) < length (t))
37 tmp = s;
38 s = t;
39 t = tmp;
40 endif
41
42 s = toascii (s);
43 t = toascii (t);
44
45 ind = 1 : length (t);
46 limit = length (s) - length (t) + 1;
47 v = zeros (1, limit);
48 i = 0;
49
50 for k = 1 : limit
51 if (s (ind + k - 1) == t)
52 v (++i) = k;
53 endif
54 endfor
55
56 if (i > 0)
57 v = v (1:i);
58 else
59 v = [];
60 endif
61
62 else
63 error ("findstr: both arguments must be strings");
64 endif
65
66 endfunction