time | calls | line |
---|
| | 1 | function [token, remainder] = strtok(string, delimiters)
|
| | 2 | %STRTOK Find token in string.
|
| | 3 | % TOKEN = STRTOK(STR) returns the first token in the string STR delimited
|
| | 4 | % by white-space characters. STRTOK ignores any leading white space.
|
| | 5 | % If STR is a cell array of strings, TOKEN is a cell array of tokens.
|
| | 6 | %
|
| | 7 | % TOKEN = STRTOK(STR,DELIM) returns the first token delimited by one of
|
| | 8 | % the characters in DELIM. STRTOK ignores any leading delimiters.
|
| | 9 | % Do not use escape sequences as delimiters. For example, use char(9)
|
| | 10 | % rather than '\t' for tab.
|
| | 11 | %
|
| | 12 | % [TOKEN,REMAIN] = STRTOK(...) returns the remainder of the original
|
| | 13 | % string.
|
| | 14 | %
|
| | 15 | % If the body of the input string does not contain any delimiter
|
| | 16 | % characters, STRTOK returns the entire string in TOKEN (excluding any
|
| | 17 | % leading delimiter characters), and REMAIN contains an empty string.
|
| | 18 | %
|
| | 19 | % Example:
|
| | 20 | %
|
| | 21 | % s = ' This is a simple example.';
|
| | 22 | % [token, remain] = strtok(s)
|
| | 23 | %
|
| | 24 | % returns
|
| | 25 | %
|
| | 26 | % token =
|
| | 27 | % This
|
| | 28 | % remain =
|
| | 29 | % is a simple example.
|
| | 30 | %
|
| | 31 | % See also ISSPACE, STRFIND, STRNCMP, STRCMP, TEXTSCAN.
|
| | 32 |
|
| | 33 | % Copyright 1984-2009 The MathWorks, Inc.
|
| | 34 |
|
| 1 | 35 | if nargin<1
|
| | 36 | error(message('MATLAB:strtok:NrInputArguments'));
|
| | 37 | end
|
| | 38 |
|
| 1 | 39 | token = ''; remainder = '';
|
| | 40 |
|
| 1 | 41 | len = length(string);
|
| 1 | 42 | if len == 0
|
| | 43 | return
|
| | 44 | end
|
| | 45 |
|
| 1 | 46 | if (nargin == 1)
|
| 1 | 47 | delimiters = [9:13 32]; % White space characters
|
| 1 | 48 | end
|
| | 49 |
|
| 1 | 50 | i = 1;
|
| 1 | 51 | while (any(string(i) == delimiters))
|
| | 52 | i = i + 1;
|
| | 53 | if (i > len),
|
| | 54 | return,
|
| | 55 | end
|
| | 56 | end
|
| | 57 |
|
| 1 | 58 | start = i;
|
| 1 | 59 | while (~any(string(i) == delimiters))
|
| 11 | 60 | i = i + 1;
|
| 11 | 61 | if (i > len),
|
| | 62 | break,
|
| | 63 | end
|
| 11 | 64 | end
|
| 1 | 65 | finish = i - 1;
|
| | 66 |
|
| 1 | 67 | token = string(start:finish);
|
| | 68 |
|
| 1 | 69 | if (nargout == 2)
|
| 1 | 70 | remainder = string(finish + 1:length(string));
|
| 1 | 71 | end
|