strtok
Extracts the first token of a text and returns the rest.
Category: Language Fundamentals > Data Types
Syntax
tok = strtok(str)
Returns the first token, delimited by whitespace.
[tok, rem] = strtok(str, delimiters)
Uses the given delimiter characters and also returns the remainder.
Description
Leading delimiters are skipped before the token starts. The remainder keeps the delimiter that ended the token, so strtok('a,b,c', ',') returns 'a' and ',b,c'. Calling strtok again on the remainder walks through the text one token at a time.
For splitting everything at once, use strsplit.
Input arguments
str- Character vector or string.
delimiters- Characters that separate tokens. Default is whitespace.
Output arguments
tok- The text from the first non-delimiter up to the next delimiter.
rem- The rest of
str, starting at the delimiter that ended the token.
Example
Tokens and remainders
[t, r] = strtok('hello world')
[t2, r2] = strtok('a,b,c', ',')
[t3, r3] = strtok(' padded text')
Output
t = 'hello'
r = ' world'
t2 = 'a'
r2 = ',b,c'
t3 = 'padded'
r3 = ' text'