Code covered by the BSD License  

Highlights from
String Toolkits

Be the first to rate this file! 137 Downloads (last 30 days) File Size: 13.7 KB File ID: #21710

String Toolkits

by

 

09 Oct 2008 (Updated )

A set of useful string manipulation functions that MATLAB does not offer

| Watch this File

File Information
Description

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.

Acknowledgements

This file inspired Cad Apps.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
22 Dec 2012 Eric Cousineau

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

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

15 Oct 2008 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.

14 Oct 2008 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.
1) STRJOIN:
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

Contact us