Navigation

Operators and Keywords

Function List:

C++ API

: [tok, rem] = strtok (str)
: [tok, rem] = strtok (str, delim)

Find all characters in the string str up to, but not including, the first character which is in the string delim.

str may also be a cell array of strings in which case the function executes on every individual string and returns a cell array of tokens and remainders.

Leading delimiters are ignored. If delim is not specified, whitespace is assumed.

If rem is requested, it contains the remainder of the string, starting at the first delimiter.

Examples:

strtok ("this is the life")
     ⇒ "this"

[tok, rem] = strtok ("14*27+31", "+-*/")
     ⇒
        tok = 14
        rem = *27+31

See also: index, strsplit, strchr, isspace.

Demonstration 1

The following code

 strtok ("this is the life")
 # split at the first space, returning "this"

Produces the following output

ans = this

Demonstration 2

The following code

 s = "14*27+31"
 while (1)
   [t, s] = strtok (s, "+-*/");
   printf ("<%s>", t);
   if (isempty (s))
     break;
   endif
   printf ("<%s>", s(1));
 endwhile
 printf ("\n");
 % ----------------------------------------------------
 % Demonstrates processing of an entire string split on
 % a variety of delimiters.  Tokens and delimiters are
 % printed one after another in angle brackets.

Produces the following output

s = 14*27+31
<14><*><27><+><31>

Package: octave