MATLAB Function Reference |
Return selected parts of string
Syntax
Description
token = strtok('str')
uses the default delimiters, the white-space characters. These include tabs (ASCII 9), carriage returns (ASCII 13), and spaces (ASCII 32). Any leading white-space characters are ignored. If str
is a cell array of strings, token
is a cell array of tokens.
token = strtok('str', delimiter)
returns the first token in the text string str
, that is, the first set of characters before a delimiter is encountered. The vector delimiter
contains valid delimiter characters. Any leading delimiters are ignored. If str
is a cell array of strings, token
is a cell array of tokens.
[token, rem] = strtok(...)
returns the remainder rem
of the original string. The remainder consists of all characters from the first delimiter on. If str
is a cell array of strings, token
is a cell array of tokens, and rem
is a character array.
Example 1
This example uses the default white-space delimiter:
Example 2
Take a string of HTML code and break it down into segments delimited by the <
and >
characters. Write a while
loop to parse the string and print each segment:
s = sprintf('%s%s%s', ... '<ul class=continued><li class=continued><pre>', ... '<a name="13474"></a>token = strtok(''str'', delimiter)', ... '<a name="13475"></a>token = strtok(''str'')'); rem = s; while true [str, rem] = strtok(rem, '<>'); if isempty(str), break; end disp(sprintf('%s', str)) end
ul class=continued li class=continued pre a name="13474" /a token = strtok('str', delimiter) a name="13475" /a token = strtok('str')
Example 3
Using strtok
on a cell array of strings returns a cell array of strings in token
and a character array in rem
:
s = {'all in good time'; ... 'my dog has fleas'; ... 'leave no stone unturned'}; rem = s; for k = 1:4 [token, rem] = strtok(rem); token end
token = 'all' 'my' 'leave' token = 'in' 'dog' 'no' token = 'good' 'has' 'stone' token = 'time' 'fleas' 'unturned'
See Also
strrep | strtrim |
© 1994-2005 The MathWorks, Inc.