| MATLAB Function Reference | ![]() |
token = strtok('str')
token = strtok('str', delimiter)
[token, remain] = strtok('str', ...)
token = strtok('str') returns in token that part of the input string str that precedes the first white-space character (the default delimiter). Parsing of the string begins at the first nondelimiting (i.e., nonwhite-space) character and continues to the right until the MATLAB® software either locates a delimiter or reaches the end of the string. If no delimiters are found in the body of the input string, then the entire string (excluding any leading delimiting characters) is returned.
White-space characters include space (ASCII 32), tab (ASCII 9), and carriage return (ASCII 13).
If str is a cell array of strings, token is a cell array of tokens.
token = strtok('str', delimiter) [4] is the same as the above syntax except that you can specify one or more nondefault delimiters in the character vector, delimiter. Ignoring any leading delimiters, MATLAB returns in token that part of the input string that precedes one of the characters from the given delimiter vector.
[token, remain] = strtok('str', ...) returns in remain a substring of the input string that begins immediately after the token substring and ends with the last character in str. If no delimiters are found in the body of the input string, then the entire string (excluding any leading delimiting characters) is returned in token, and remain is an empty string ('').
If str is a cell array of strings, token is a cell array of tokens and remain is a character array.
This example uses the default white-space delimiter:
s = ' This is a simple example.'; [token, remain] = strtok(s) token = This remain = is a simple example.
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%s', ...
'<ul class=continued><li class=continued>', ...
'<pre><a name="13474"></a>token = strtok', ...
'(''str'', delimiter)<a name="13475"></a>', ...
'token = strtok(''str'')');
remain = s;
while true
[str, remain] = strtok(remain, '<>');
if isempty(str), break; end
disp(sprintf('%s', str))
end
Here is the output:
ul class=continued
li class=continued
pre
a name="13474"
/a
token = strtok('str', delimiter)
a name="13475"
/a
token = strtok('str')
Using strtok on a cell array of strings returns a cell array of strings in token and a character array in remain:
s = {'all in good time'; ...
'my dog has fleas'; ...
'leave no stone unturned'};
remain = s;
for k = 1:4
[token, remain] = strtok(remain);
token
end
Here is the output:
token =
'all'
'my'
'leave'
token =
'in'
'dog'
'no'
token =
'good'
'has'
'stone'
token =
'time'
'fleas'
'unturned'
![]() | strrep | strtrim | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |