explode_implode

Two functions: split string into pieces, join strings with delimiter in between.
3.8K Downloads
Updated 11 Mar 2005

View License

%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!)

Cite As

Sara Silva (2024). explode_implode (https://www.mathworks.com/matlabcentral/fileexchange/3028-explode_implode), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R10
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers
Acknowledgements

Inspired: rsplit

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0

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!)