comparison scripts/strings/split.m @ 2274:9750746d7da5

[project @ 1996-05-24 04:06:42 by jwe] Initial revision
author jwe
date Fri, 24 May 1996 04:06:42 +0000
parents
children 6dedd4e0a82f
comparison
equal deleted inserted replaced
2273:5bc8dd06898d 2274:9750746d7da5
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 m = split (s, t)
20
21 # usage: m = split (s, t)
22 #
23 # Divides the string S into pieces separated by T, and stores the
24 # pieces as the rows of M (padded with blanks to form a valid
25 # matrix).
26
27 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>.
28
29 if (nargin != 2)
30 usage ("split (s, t)");
31 endif
32
33 if (isstr (s) && isstr (t))
34
35 l_s = length (s);
36 l_t = length (t);
37
38 if (l_s < l_t)
39 error ("split: s must not be shorter than t");
40 endif
41
42 if (l_t == 0)
43 ind = 1 : (l_s + 1);
44 else
45 ind = findstr (s, t, 0);
46 if (length (ind) == 0)
47 m = s;
48 return;
49 endif
50 ind = [1 - l_t, ind, l_s + 1];
51 endif
52
53 cmd = "";
54
55 limit = length (ind) - 1;
56
57 for k = 1 : limit
58
59 range = (ind (k) + l_t) : ind (k + 1) - 1;
60
61 if (k != limit)
62 cmd = sprintf ("%s\"%s\", ", cmd, s (range));
63 else
64 cmd = sprintf ("%s\"%s\"", cmd, s (range));
65 endif
66
67 endfor
68
69 m = eval (sprintf ("str2mat (%s);", cmd));
70
71
72 else
73 error ("split: both s and t must be strings");
74 endif
75
76 endfunction