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 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} split (@var{s}, @var{t}) |
|
22 ## Divides the string @var{s} into pieces separated by @var{t}, returning |
|
23 ## the result in a string array (padded with blanks to form a valid |
|
24 ## matrix). For example, |
3426
|
25 ## |
3361
|
26 ## @example |
|
27 ## split ("Test string", "t") |
|
28 ## @result{} "Tes " |
|
29 ## " s " |
|
30 ## "ring" |
|
31 ## @end example |
|
32 ## @end deftypefn |
2311
|
33 |
2355
|
34 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
35 ## Adapted-By: jwe |
2314
|
36 |
2274
|
37 function m = split (s, t) |
2325
|
38 |
2274
|
39 if (nargin != 2) |
|
40 usage ("split (s, t)"); |
|
41 endif |
2325
|
42 |
5218
|
43 if not(ischar (s) && ischar (t)) |
|
44 error ("split: both s and t must be strings"); |
|
45 endif |
|
46 |
2325
|
47 |
2274
|
48 l_s = length (s); |
|
49 l_t = length (t); |
2325
|
50 |
3320
|
51 if (l_s == 0) |
|
52 m = ""; |
|
53 return; |
5218
|
54 elseif (l_t == 0) |
|
55 m = s'; |
|
56 return; |
3320
|
57 elseif (l_s < l_t) |
2274
|
58 error ("split: s must not be shorter than t"); |
|
59 endif |
5218
|
60 |
|
61 if (min(size(s)) ~= 1 | min(size(t)) ~= 1) |
|
62 error("split: multible strings are not supported"); |
2274
|
63 endif |
|
64 |
5218
|
65 ind = findstr (s, t, 0); |
|
66 if (length (ind) == 0) |
|
67 m = s; |
|
68 return; |
|
69 endif |
|
70 ind2 = [1, ind+l_t]; |
|
71 ind = [ind, l_s+1]; |
2274
|
72 |
5218
|
73 ind_diff = ind-ind2; |
|
74 % Create a matrix of the correct size that's filled with spaces |
|
75 m_rows = length(ind); |
|
76 m_cols = max(ind_diff); |
|
77 m = char( zeros(m_rows, m_cols) + ' ' ); |
2274
|
78 |
5218
|
79 % Copy the strings to the matrix |
|
80 for i = 1:length(ind) |
|
81 tmp = ind2(i):(ind(i)-1); |
|
82 m(i, 1:length(tmp)) = s(tmp); |
|
83 end |
2274
|
84 |
|
85 endfunction |