7017
|
1 ## Copyright (C) 1996, 1999, 2000, 2005, 2006, 2007 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2274
|
18 |
3361
|
19 ## -*- texinfo -*- |
5462
|
20 ## @deftypefn {Function File} {} split (@var{s}, @var{t}, @var{n}) |
3361
|
21 ## Divides the string @var{s} into pieces separated by @var{t}, returning |
|
22 ## the result in a string array (padded with blanks to form a valid |
5462
|
23 ## matrix). If the optional input @var{n} is supplied, split @var{s} |
|
24 ## into at most @var{n} different pieces. |
|
25 ## |
|
26 ## For example, |
3426
|
27 ## |
3361
|
28 ## @example |
|
29 ## split ("Test string", "t") |
|
30 ## @result{} "Tes " |
|
31 ## " s " |
|
32 ## "ring" |
|
33 ## @end example |
5462
|
34 ## |
|
35 ## @example |
|
36 ## split ("Test string", "t", 2) |
|
37 ## @result{} "Tes " |
|
38 ## " string" |
|
39 ## @end example |
3361
|
40 ## @end deftypefn |
2311
|
41 |
5428
|
42 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
43 ## Adapted-By: jwe |
2314
|
44 |
5462
|
45 function m = split (s, t, n) |
2325
|
46 |
5462
|
47 if (nargin == 2 || nargin == 3) |
|
48 if (nargin == 2) |
|
49 n = length (s); |
|
50 endif |
|
51 |
5443
|
52 if (ischar (s) && ischar (t)) |
5219
|
53 |
|
54 l_s = length (s); |
|
55 l_t = length (t); |
|
56 |
|
57 if (l_s == 0) |
|
58 m = ""; |
|
59 return; |
|
60 elseif (l_t == 0) |
|
61 m = s'; |
|
62 return; |
|
63 elseif (l_s < l_t) |
|
64 error ("split: s must not be shorter than t"); |
|
65 endif |
|
66 |
|
67 if (min (size (s)) != 1 || min (size (t)) != 1) |
|
68 error("split: multi-line strings are not supported"); |
|
69 endif |
|
70 |
|
71 ind = findstr (s, t, 0); |
|
72 if (length (ind) == 0) |
|
73 m = s; |
|
74 return; |
5462
|
75 elseif (n - 1 < length(ind)) |
5473
|
76 ind = ind(1:n-1); |
5219
|
77 endif |
|
78 ind2 = [1, ind+l_t]; |
|
79 ind = [ind, l_s+1]; |
|
80 |
|
81 ind_diff = ind-ind2; |
|
82 |
|
83 ## Create a matrix of the correct size that's filled with spaces. |
|
84 m_rows = length (ind); |
|
85 m_cols = max (ind_diff); |
|
86 m = repmat (" ", m_rows, m_cols); |
|
87 |
|
88 ## Copy the strings to the matrix. |
|
89 for i = 1:length (ind) |
|
90 tmp = ind2(i):(ind(i)-1); |
|
91 m(i,1:length(tmp)) = s(tmp); |
|
92 endfor |
|
93 else |
|
94 error ("split: both s and t must be strings"); |
|
95 endif |
|
96 else |
6046
|
97 print_usage (); |
2274
|
98 endif |
2325
|
99 |
2274
|
100 endfunction |