Home > vbmeg > functions > common > utility > explode.m

explode

PURPOSE ^

EXPLODE Splits string into pieces.

SYNOPSIS ^

function [split,numpieces]=explode(string,delimiters)

DESCRIPTION ^

EXPLODE    Splits string into pieces.
   EXPLODE(STRING,DELIMITERS) returns a cell array with the pieces
   of STRING found between any of the characters in DELIMITERS.

   [SPLIT,NUMPIECES] = EXPLODE(STRING,DELIMITERS) also returns the
   number of pieces found in STRING.

   Input arguments:
      STRING - the string to split (string)
      DELIMITERS - the delimiter characters (string)
   Output arguments:
      SPLIT - the split string (cell array), each cell is a piece
      NUMPIECES - the number of pieces found (integer)

   Example:
      STRING = 'ab_c,d,e fgh'
      DELIMITERS = '_,'
      [SPLIT,NUMPIECES] = EXPLODE(STRING,DELIMITERS)
      SPLIT = 'ab'    'c'    'd'    'e fgh'
      NUMPIECES = 4

   See also IMPLODE, STRTOK

   Created: Sara Silva (sara@itqb.unl.pt) - 2002.04.30

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [split,numpieces]=explode(string,delimiters)
0002 %EXPLODE    Splits string into pieces.
0003 %   EXPLODE(STRING,DELIMITERS) returns a cell array with the pieces
0004 %   of STRING found between any of the characters in DELIMITERS.
0005 %
0006 %   [SPLIT,NUMPIECES] = EXPLODE(STRING,DELIMITERS) also returns the
0007 %   number of pieces found in STRING.
0008 %
0009 %   Input arguments:
0010 %      STRING - the string to split (string)
0011 %      DELIMITERS - the delimiter characters (string)
0012 %   Output arguments:
0013 %      SPLIT - the split string (cell array), each cell is a piece
0014 %      NUMPIECES - the number of pieces found (integer)
0015 %
0016 %   Example:
0017 %      STRING = 'ab_c,d,e fgh'
0018 %      DELIMITERS = '_,'
0019 %      [SPLIT,NUMPIECES] = EXPLODE(STRING,DELIMITERS)
0020 %      SPLIT = 'ab'    'c'    'd'    'e fgh'
0021 %      NUMPIECES = 4
0022 %
0023 %   See also IMPLODE, STRTOK
0024 %
0025 %   Created: Sara Silva (sara@itqb.unl.pt) - 2002.04.30
0026 
0027 if isempty(string) % empty string, return empty and 0 pieces
0028    split{1}='';
0029    numpieces=0;
0030    
0031 elseif isempty(delimiters) % no delimiters, return whole string in 1 piece
0032    split{1}=string;
0033    numpieces=1;
0034    
0035 else % non-empty string and delimiters, the correct case
0036    
0037    remainder=string;
0038    i=0;
0039    
0040     while ~isempty(remainder)
0041        [piece,remainder]=strtok(remainder,delimiters);
0042        i=i+1;
0043        split{i}=piece;
0044     end
0045    numpieces=i;
0046    
0047 end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005