comparison toolboxes/FullBNT-1.0.7/KPMtools/strsplit.m @ 0:e9a9cd732c1e tip

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