2313
|
1 ## Copyright (C) 1996 Kurt Hornik |
|
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 |
|
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 |
2274
|
26 function m = split (s, t) |
|
27 |
2311
|
28 ## Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. |
2274
|
29 |
|
30 if (nargin != 2) |
|
31 usage ("split (s, t)"); |
|
32 endif |
|
33 |
|
34 if (isstr (s) && isstr (t)) |
|
35 |
|
36 l_s = length (s); |
|
37 l_t = length (t); |
|
38 |
|
39 if (l_s < l_t) |
|
40 error ("split: s must not be shorter than t"); |
|
41 endif |
|
42 |
|
43 if (l_t == 0) |
|
44 ind = 1 : (l_s + 1); |
|
45 else |
|
46 ind = findstr (s, t, 0); |
|
47 if (length (ind) == 0) |
|
48 m = s; |
|
49 return; |
|
50 endif |
|
51 ind = [1 - l_t, ind, l_s + 1]; |
|
52 endif |
|
53 |
|
54 cmd = ""; |
|
55 |
|
56 limit = length (ind) - 1; |
|
57 |
|
58 for k = 1 : limit |
|
59 |
|
60 range = (ind (k) + l_t) : ind (k + 1) - 1; |
|
61 |
|
62 if (k != limit) |
|
63 cmd = sprintf ("%s\"%s\", ", cmd, s (range)); |
|
64 else |
|
65 cmd = sprintf ("%s\"%s\"", cmd, s (range)); |
|
66 endif |
|
67 |
|
68 endfor |
|
69 |
|
70 m = eval (sprintf ("str2mat (%s);", cmd)); |
|
71 |
|
72 |
|
73 else |
|
74 error ("split: both s and t must be strings"); |
|
75 endif |
|
76 |
|
77 endfunction |