wolffd@0
|
1 function parts = strsplit(splitstr, str, option)
|
wolffd@0
|
2 %STRSPLIT Split string into pieces.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % STRSPLIT(SPLITSTR, STR, OPTION) splits the string STR at every occurrence
|
wolffd@0
|
5 % of SPLITSTR and returns the result as a cell array of strings. By default,
|
wolffd@0
|
6 % SPLITSTR is not included in the output.
|
wolffd@0
|
7 %
|
wolffd@0
|
8 % STRSPLIT(SPLITSTR, STR, OPTION) can be used to control how SPLITSTR is
|
wolffd@0
|
9 % included in the output. If OPTION is 'include', SPLITSTR will be included
|
wolffd@0
|
10 % as a separate string. If OPTION is 'append', SPLITSTR will be appended to
|
wolffd@0
|
11 % each output string, as if the input string was split at the position right
|
wolffd@0
|
12 % after the occurrence SPLITSTR. If OPTION is 'omit', SPLITSTR will not be
|
wolffd@0
|
13 % included in the output.
|
wolffd@0
|
14
|
wolffd@0
|
15 % Author: Peter J. Acklam
|
wolffd@0
|
16 % Time-stamp: 2004-09-22 08:48:01 +0200
|
wolffd@0
|
17 % E-mail: pjacklam@online.no
|
wolffd@0
|
18 % URL: http://home.online.no/~pjacklam
|
wolffd@0
|
19
|
wolffd@0
|
20 nargsin = nargin;
|
wolffd@0
|
21 error(nargchk(2, 3, nargsin));
|
wolffd@0
|
22 if nargsin < 3
|
wolffd@0
|
23 option = 'omit';
|
wolffd@0
|
24 else
|
wolffd@0
|
25 option = lower(option);
|
wolffd@0
|
26 end
|
wolffd@0
|
27
|
wolffd@0
|
28 splitlen = length(splitstr);
|
wolffd@0
|
29 parts = {};
|
wolffd@0
|
30
|
wolffd@0
|
31 while 1
|
wolffd@0
|
32
|
wolffd@0
|
33 k = strfind(str, splitstr);
|
wolffd@0
|
34 if isempty(k)
|
wolffd@0
|
35 parts{end+1} = str;
|
wolffd@0
|
36 break
|
wolffd@0
|
37 end
|
wolffd@0
|
38
|
wolffd@0
|
39 switch option
|
wolffd@0
|
40 case 'include'
|
wolffd@0
|
41 parts(end+1:end+2) = {str(1:k(1)-1), splitstr};
|
wolffd@0
|
42 case 'append'
|
wolffd@0
|
43 parts{end+1} = str(1 : k(1)+splitlen-1);
|
wolffd@0
|
44 case 'omit'
|
wolffd@0
|
45 parts{end+1} = str(1 : k(1)-1);
|
wolffd@0
|
46 otherwise
|
wolffd@0
|
47 error(['Invalid option string -- ', option]);
|
wolffd@0
|
48 end
|
wolffd@0
|
49
|
wolffd@0
|
50
|
wolffd@0
|
51 str = str(k(1)+splitlen : end);
|
wolffd@0
|
52
|
wolffd@0
|
53 end
|