Home > utility > split.m

split

PURPOSE ^

split: Split a string based on a given delimiter

SYNOPSIS ^

function tokenList = split(str, delimiter, mode)

DESCRIPTION ^

 split: Split a string based on a given delimiter
    Usage: tokenList = split(str, delimiter, mode)

    For example:
        str='--This-is---a-test--';
        fprintf('str = "%s"\n', str);
        tokenList=split(str, '-', 0);
        fprintf('After running "tokenList = split(str, ''-'', 0)":\n');
        strVecPrint(tokenList, 'tokenList');
        tokenList=split(str, '-', 1);
        fprintf('After running "tokenList = split(str, ''-'', 1)":\n');
        strVecPrint(tokenList, 'tokenList');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function tokenList = split(str, delimiter, mode)
0002 % split: Split a string based on a given delimiter
0003 %    Usage: tokenList = split(str, delimiter, mode)
0004 %
0005 %    For example:
0006 %        str='--This-is---a-test--';
0007 %        fprintf('str = "%s"\n', str);
0008 %        tokenList=split(str, '-', 0);
0009 %        fprintf('After running "tokenList = split(str, ''-'', 0)":\n');
0010 %        strVecPrint(tokenList, 'tokenList');
0011 %        tokenList=split(str, '-', 1);
0012 %        fprintf('After running "tokenList = split(str, ''-'', 1)":\n');
0013 %        strVecPrint(tokenList, 'tokenList');
0014 
0015 %    See also JOIN.
0016 
0017 %    Roger Jang, 20010324, 20071009
0018 
0019 if nargin<1; selfdemo; return; end
0020 if nargin<2; delimiter=' '; end
0021 if nargin<3; mode=0; end
0022 
0023 if length(str)==0
0024     tokenList={};
0025     return;
0026 end
0027 
0028 switch(mode)
0029     case 0
0030         tokenList = {};
0031         remain = str;
0032         i = 1;
0033         while ~isempty(remain),
0034             [token, remain] = strtok(remain, delimiter);
0035             tokenList{i} = token;
0036             i = i+1;
0037         end
0038         % Get rid of the possible empty last element
0039         if isempty(tokenList{end})
0040             tokenList(end)=[];
0041         end
0042     case 1
0043         tokenList={};
0044         beginPos=1;
0045         prevDef=0;
0046         for i=1:length(str)
0047             if str(i)==delimiter
0048                 tokenList{end+1}=str(beginPos:i-1);
0049                 prefDef=0;
0050                 beginPos=i+1;
0051             end
0052             if str(i)~=delimiter & ~prevDef
0053                 beginPos=i;
0054                 prevDef=1;
0055             end 
0056         end
0057 
0058         if str(end)==delimiter
0059             tokenList{end+1}='';
0060         else
0061             tokenList{end+1}=str(beginPos:end);
0062         end
0063     otherwise
0064         error('Unknown mode!');
0065 end
0066 
0067 % ====== Self demo
0068 function selfdemo
0069 str='--This-is---a-test--';
0070 fprintf('str = "%s"\n', str);
0071 tokenList=feval(mfilename, str, '-', 0);
0072 fprintf('After running "tokenList = split(str, ''-'', 0)":\n');
0073 strVecPrint(tokenList, 'tokenList');
0074 tokenList=feval(mfilename, str, '-', 1);
0075 fprintf('After running "tokenList = split(str, ''-'', 1)":\n');
0076 strVecPrint(tokenList, 'tokenList');

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003