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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2274
|
19 |
3361
|
20 ## -*- texinfo -*- |
5462
|
21 ## @deftypefn {Function File} {} split (@var{s}, @var{t}, @var{n}) |
3361
|
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 |
5462
|
24 ## matrix). If the optional input @var{n} is supplied, split @var{s} |
|
25 ## into at most @var{n} different pieces. |
|
26 ## |
|
27 ## For example, |
3426
|
28 ## |
3361
|
29 ## @example |
|
30 ## split ("Test string", "t") |
|
31 ## @result{} "Tes " |
|
32 ## " s " |
|
33 ## "ring" |
|
34 ## @end example |
5462
|
35 ## |
|
36 ## @example |
|
37 ## split ("Test string", "t", 2) |
|
38 ## @result{} "Tes " |
|
39 ## " string" |
|
40 ## @end example |
3361
|
41 ## @end deftypefn |
2311
|
42 |
5428
|
43 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
44 ## Adapted-By: jwe |
2314
|
45 |
5462
|
46 function m = split (s, t, n) |
2325
|
47 |
5462
|
48 if (nargin == 2 || nargin == 3) |
|
49 if (nargin == 2) |
|
50 n = length (s); |
|
51 endif |
|
52 |
5443
|
53 if (ischar (s) && ischar (t)) |
5219
|
54 |
|
55 l_s = length (s); |
|
56 l_t = length (t); |
|
57 |
|
58 if (l_s == 0) |
|
59 m = ""; |
|
60 return; |
|
61 elseif (l_t == 0) |
|
62 m = s'; |
|
63 return; |
|
64 elseif (l_s < l_t) |
|
65 error ("split: s must not be shorter than t"); |
|
66 endif |
|
67 |
|
68 if (min (size (s)) != 1 || min (size (t)) != 1) |
|
69 error("split: multi-line strings are not supported"); |
|
70 endif |
|
71 |
|
72 ind = findstr (s, t, 0); |
|
73 if (length (ind) == 0) |
|
74 m = s; |
|
75 return; |
5462
|
76 elseif (n - 1 < length(ind)) |
5473
|
77 ind = ind(1:n-1); |
5219
|
78 endif |
|
79 ind2 = [1, ind+l_t]; |
|
80 ind = [ind, l_s+1]; |
|
81 |
|
82 ind_diff = ind-ind2; |
|
83 |
|
84 ## Create a matrix of the correct size that's filled with spaces. |
|
85 m_rows = length (ind); |
|
86 m_cols = max (ind_diff); |
|
87 m = repmat (" ", m_rows, m_cols); |
|
88 |
|
89 ## Copy the strings to the matrix. |
|
90 for i = 1:length (ind) |
|
91 tmp = ind2(i):(ind(i)-1); |
|
92 m(i,1:length(tmp)) = s(tmp); |
|
93 endfor |
|
94 else |
|
95 error ("split: both s and t must be strings"); |
|
96 endif |
|
97 else |
6046
|
98 print_usage (); |
2274
|
99 endif |
2325
|
100 |
2274
|
101 endfunction |