File Exchange

image thumbnail

String Toolkits

version (13.7 KB) by Dahua Lin
A set of useful string manipulation functions that MATLAB does not offer


Updated 10 Oct 2008

View License

It is a collection of useful string manipulation functions, which offer versatile functionality for joining, splitting, translating strings.

MATLAB is shipped with a series of string functions. However, some useful functions have not yet been in the MATLAB releases. Inspired by other advanced languages like Java, Ruby, and Python, I developed a set of string manipulation functions in order to make string processing more convenient.

Here gives a brief list of contents

strjoin: joins multiple strings with user-specified delimiter

strsplit: extracts terms from a string that are separated by a specified delimiter

strsplit_re: similar to strsplit, but the delimiter is given by regular expression.

strstartswith, strendswith: determine whether a string starts or ends with a particular pattern

strgsub: translates parts of strings in a user-defined way.

This toolbox is not aimed at offering a comprehensive string library and replacing the string functions in MATLAB. Its target is just to complement what MATLAB is lacking. Hope it helps.

Comments and Ratings (7)



Here is an extension to strstartswith to handle cellstr arguments for 'pat':

nPat = length(pat);
if ~iscell(pat)
result = strncmp(s, pat, nPat);
result = false(1, nPat);
for i = 1:nPat
result(i) = strncmp(s, pat{i}, length(pat{i}));

Dahua Lin

Thanks for Jos x's comments. I agree that most of the functionality that the toolkit offers can be accomplished with MATLAB's own functions in several lines.

However, when I wrote something related to text processing in MATLAB, I found that these tasks are repeatedly required, and thus it looks cumbersome to repeat those codes again and again, though they are not very long.

Some I organize these tasks into functions and it turns out that they make the text-processing code substantially shorter and more readable.

STRSPLIT actually targets a different situation from textscan. textscan is for finding substrings following some patterns, while strsplit and strsplit_re don't care about each substring's format, and mainly focus on locating the delimiters. They would be sometimes useful when you are intending to extract items from a list.

Jos x@y,z

These are nice but simple wrapper functions for some common tasks. There is sufficient help, but the files could do with some more internal comments.
However, not all functionality is lacking in matlab itself and there are possible suggestions for improvements in the present submission.
s(1:2:2*numel(terms)) = terms ;
s(2:2:end) = {delimiter} ;
s = [s{:}] ;

2) STRSPLIT has no advantage over TEXTSCAN

3) STRGSUB is akin to STRREP and STRREGEXP; the use of anonymous function in strgsub could benefit from a better description and more examples

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux