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. |
2274
|
19 |
2311
|
20 ## usage: m = split (s, t) |
|
21 ## |
|
22 ## Divides the string S into pieces separated by T, and stores the |
|
23 ## pieces as the rows of M (padded with blanks to form a valid |
|
24 ## matrix). |
|
25 |
2355
|
26 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
27 ## Adapted-By: jwe |
2314
|
28 |
2274
|
29 function m = split (s, t) |
2325
|
30 |
2274
|
31 if (nargin != 2) |
|
32 usage ("split (s, t)"); |
|
33 endif |
2325
|
34 |
2274
|
35 if (isstr (s) && isstr (t)) |
2325
|
36 |
2274
|
37 l_s = length (s); |
|
38 l_t = length (t); |
2325
|
39 |
2274
|
40 if (l_s < l_t) |
|
41 error ("split: s must not be shorter than t"); |
|
42 endif |
2325
|
43 |
2274
|
44 if (l_t == 0) |
|
45 ind = 1 : (l_s + 1); |
|
46 else |
|
47 ind = findstr (s, t, 0); |
|
48 if (length (ind) == 0) |
|
49 m = s; |
|
50 return; |
|
51 endif |
|
52 ind = [1 - l_t, ind, l_s + 1]; |
|
53 endif |
|
54 |
|
55 cmd = ""; |
|
56 |
|
57 limit = length (ind) - 1; |
|
58 |
|
59 for k = 1 : limit |
|
60 |
|
61 range = (ind (k) + l_t) : ind (k + 1) - 1; |
|
62 |
|
63 if (k != limit) |
|
64 cmd = sprintf ("%s\"%s\", ", cmd, s (range)); |
|
65 else |
|
66 cmd = sprintf ("%s\"%s\"", cmd, s (range)); |
|
67 endif |
|
68 |
|
69 endfor |
|
70 |
|
71 m = eval (sprintf ("str2mat (%s);", cmd)); |
2325
|
72 |
2274
|
73 |
|
74 else |
|
75 error ("split: both s and t must be strings"); |
|
76 endif |
|
77 |
|
78 endfunction |