Home > source > strsplit.m

strsplit

PURPOSE ^

Split string into pieces.

SYNOPSIS ^

function parts = strsplit( splitstr, str, option )

DESCRIPTION ^

Split string into pieces.

STRSPLIT(SPLITSTR, STR, OPTION) splits the string STR at every occurrence
of SPLITSTR and returns the result as a cell array of strings.  By default,
SPLITSTR is not included in the output.

STRSPLIT(SPLITSTR, STR, OPTION) can be used to control how SPLITSTR is
included in the output.  If OPTION is 'include', SPLITSTR will be included
as a separate string.  If OPTION is 'append', SPLITSTR will be appended to
each output string, as if the input string was split at the position right
after the occurrence SPLITSTR.  If OPTION is 'omit', SPLITSTR will not be
included in the output.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %Split string into pieces.
0002 %
0003 %STRSPLIT(SPLITSTR, STR, OPTION) splits the string STR at every occurrence
0004 %of SPLITSTR and returns the result as a cell array of strings.  By default,
0005 %SPLITSTR is not included in the output.
0006 %
0007 %STRSPLIT(SPLITSTR, STR, OPTION) can be used to control how SPLITSTR is
0008 %included in the output.  If OPTION is 'include', SPLITSTR will be included
0009 %as a separate string.  If OPTION is 'append', SPLITSTR will be appended to
0010 %each output string, as if the input string was split at the position right
0011 %after the occurrence SPLITSTR.  If OPTION is 'omit', SPLITSTR will not be
0012 %included in the output.
0013 
0014 %   Author:      Peter J. Acklam
0015 %   Time-stamp:  2004-09-22 08:48:01 +0200
0016 %   E-mail:      pjacklam@online.no
0017 %   URL:         http://home.online.no/~pjacklam
0018 
0019 function parts = strsplit( splitstr, str, option )
0020 
0021    nargsin = nargin;
0022    error(nargchk(2, 3, nargsin));
0023    if nargsin < 3
0024       option = 'omit';
0025    else
0026       option = lower(option);
0027    end
0028 
0029    splitlen = length(splitstr);
0030    parts = {};
0031 
0032    while 1
0033 
0034       k = strfind(str, splitstr);
0035       if isempty(k)
0036          parts{end+1} = str;
0037          break
0038       end
0039 
0040       switch option
0041          case 'include'
0042             parts(end+1:end+2) = {str(1:k(1)-1), splitstr};
0043          case 'append'
0044             parts{end+1} = str(1 : k(1)+splitlen-1);
0045          case 'omit'
0046             parts{end+1} = str(1 : k(1)-1);
0047          otherwise
0048             error(['Invalid option string -- ', option]);
0049       end
0050 
0051 
0052       str = str(k(1)+splitlen : end);
0053 
0054    end

Generated on Thu 24-Sep-2009 17:17:32 by m2html © 2005