Documentation Center

  • Trials
  • Product Updates

strtok

Selected parts of string

Syntax

token = strtok(str)
token = strtok(str, delimiter)
[token, remain] = strtok(str, ...)

Description

token = strtok(str) parses input string str from left to right, returning part or all of that string in token. Using the white-space character as a delimiter, the token output begins at the start of str, skipping any delimiters that might appear at the start, and includes all characters up to either the next delimiter or the end of the string. White-space characters include space (ASCII 32), tab (ASCII 9), and carriage return (ASCII 13).

The str argument can be a string of characters enclosed in single quotation marks, a cell array of strings each enclosed in single quotation marks, or a variable representing either of the two. If str is a cell array of N strings, then token is a cell array of N tokens, with token{1} derived from str{1}, token{2} from str{2}, and so on.

token = strtok(str, delimiter) is the same as the above syntax except that you specify the delimiting character(s) yourself using the delimiter character vector input. White-space characters are not considered to be delimiters when using this syntax unless you include them in the delimiter argument. If the delimiter input specifies more than one character, MATLAB® treats each character as a separate delimiter; it does not treat the multiple characters as a delimiting string. The number and order of characters in the delimiter argument is unimportant. Do not use escape sequences as delimiters. For example, use char(9) rather than '\t' for tab.

[token, remain] = strtok(str, ...) returns in remain that part of str, if any, that follows token. The delimiter is included in remain. 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 cell array of string remainders.

Examples

Example 1

This example uses the default white-space delimiter. Note that space characters at the start of the string are not included in the token output, but the space character that follows token is included in remain:

s = '  This is a simple example.';
[token, remain] = strtok(s)

token =
This
remain =
 is a simple example.

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

Example 3

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'

See Also

| | | |

Was this topic helpful?