Code covered by the BSD License  

Highlights from
explode_implode

4.42857

4.4 | 7 ratings Rate this file 25 Downloads (last 30 days) File Size: 2.29 KB File ID: #3028

explode_implode

by Sara Silva

 

17 Feb 2003 (Updated 11 Mar 2005)

Two functions: split string into pieces, join strings with delimiter in between.

| Watch this File

File Information
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

%IMPLODE Joins strings with delimiter in between.
% IMPLODE(PIECES,DELIMITER) returns a string containing all the
% strings in PIECES joined with the DELIMITER string in between.
%
% Input arguments:
% PIECES - the pieces of string to join (cell array), each cell is a piece
% DELIMITER - the delimiter string to put between the pieces (string)
% Output arguments:
% STRING - all the pieces joined with the delimiter in between (string)
%
% Example:
% PIECES = {'ab','c','d','e fgh'}
% DELIMITER = '->'
% STRING = IMPLODE(PIECES,DELIMITER)
% STRING = ab->c->d->e fgh
%
% See also EXPLODE, STRCAT
%
% Created: Sara Silva (sara@itqb.unl.pt) - 2002.08.25
% Modified: Sara Silva (sara@dei.uc.pt) - 2005.03.11
% - implode did not work if the delimiter was whitespace, so
% line 36 was replaced by line 37.
% - thank you to Matthew Davidson for pointing this out
% (and providing the solution!)

Acknowledgements
This submission has inspired the following:
rsplit
MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
20 Feb 2004 Fahad Al Mahmood

Very simple yet very useful function indeed! Great job ....

16 Apr 2004 Michaela Mitschke

Works perfect - what a convenient idea to pass strings around in function arguments!

(Especially when those are then calling external skripts, with a varying number of arguments...)

01 Aug 2005 Kester Allen

Great implementation!

12 Nov 2006 Janine Campbell

Very useful.

03 Jan 2007 Ratnakar Vejella

Just what i wanted, thanks so much

13 Apr 2007 John Dorian

Very good, thanks!

15 Apr 2007 Jos x@y.z

These files can be easily vectorized.

Implode.m:
P = PIECES(:)' ;
P(2,:) = {DELIMITER} ;
P{2,end} = [] ;
string = sprintf('%s',P{:})

explode.m:
SPLIT = strread(STRING,'%s','delimiter','DELIMITER') ;

02 Oct 2010 Zhiqiang Zhang

SPLIT = strread(STRING,'%s','delimiter','DELIMITER') ;
doesn't work well. It trum off the space at the beginning of the pieces.

Please login to add a comment or rating.
Updates
21 Feb 2003

Only the description was updated. The first line in the "explode" help was missing. The functions are exactly the same.

11 Mar 2005

implode did not work if the delimiter was whitespace, so line 36 was replaced by line 37. explode remains the same.

Thank you to Matthew Davidson for pointing this out (and providing the solution!)

Tag Activity for this File
Tag Applied By Date/Time
strings Sara Silva 22 Oct 2008 06:56:59
explode Sara Silva 22 Oct 2008 06:56:59
implode Sara Silva 22 Oct 2008 06:56:59
utilities Sara Silva 22 Oct 2008 06:56:59
string manipulation Sara Silva 22 Oct 2008 06:56:59

Contact us at files@mathworks.com