time | Calls | line |
---|
| | 1 | function [c, matches] = strsplit(str, aDelim, varargin)
|
| | 2 | %STRSPLIT Split string at delimiter
|
| | 3 | % C = STRSPLIT(STR) splits the character vector or string scalar STR at
|
| | 4 | % whitespace into C.
|
| | 5 | %
|
| | 6 | % C = STRSPLIT(STR, DELIMITER) splits STR at DELIMITER into C. If
|
| | 7 | % DELIMITER is a cell array of character vectors, STRSPLIT splits STR
|
| | 8 | % along the elements in DELIMITER, in the order in which they appear in
|
| | 9 | % the cell array.
|
| | 10 | %
|
| | 11 | % C = STRSPLIT(STR, DELIMITER, PARAM1, VALUE1, ... PARAMN, VALUEN)
|
| | 12 | % modifies the way in which STR is split at DELIMITER.
|
| | 13 | %
|
| | 14 | % Valid parameters are:
|
| | 15 | %
|
| | 16 | % 'CollapseDelimiters' - If true (default), consecutive delimiters in
|
| | 17 | % STR are treated as one. If false, consecutive delimiters are
|
| | 18 | % treated as separate delimiters, resulting in text with no
|
| | 19 | % characters between matched delimiters.
|
| | 20 | % 'DelimiterType' - DelimiterType can have the following values:
|
| | 21 | % 'Simple' (default) - Except for escape sequences, STRSPLIT treats
|
| | 22 | % DELIMITER as a literal.
|
| | 23 | % 'RegularExpression' - STRSPLIT treats DELIMITER as a regular
|
| | 24 | % expression.
|
| | 25 | % In both cases, DELIMITER can include the following escape
|
| | 26 | % sequences:
|
| | 27 | % \\ Backslash \n New line
|
| | 28 | % \0 Null \r Carriage return
|
| | 29 | % \a Alarm \t Horizontal tab
|
| | 30 | % \b Backspace \v Vertical tab
|
| | 31 | % \f Form feed
|
| | 32 | %
|
| | 33 | % [C, MATCHES] = STRSPLIT(...) also returns the cell array of character
|
| | 34 | % vectors MATCHES containing the DELIMITERs upon which STR was split.
|
| | 35 | % Note that MATCHES always contains one fewer element than C.
|
| | 36 | %
|
| | 37 | % NOTE: STR can be a string array or character vector. DELIMITER can be
|
| | 38 | % a string array, character vector, or cell array of character vectors.
|
| | 39 | % When STR is a string array, outputs C and MATCHES are string arrays.
|
| | 40 | % Otherwise, C and MATCHES are cell arrays of character vectors.
|
| | 41 | %
|
| | 42 | % Examples:
|
| | 43 | %
|
| | 44 | % str = 'The rain in Spain stays mainly in the plain.';
|
| | 45 | %
|
| | 46 | % % Split on all whitespace.
|
| | 47 | % strsplit(str)
|
| | 48 | % % {'The', 'rain', 'in', 'Spain', 'stays',
|
| | 49 | % % 'mainly', 'in', 'the', 'plain.'}
|
| | 50 | %
|
| | 51 | % % Split on 'ain'.
|
| | 52 | % strsplit(str, 'ain')
|
| | 53 | % % {'The r', ' in Sp', ' stays m', 'ly in the pl', '.'}
|
| | 54 | %
|
| | 55 | % % Split on ' ' and on 'ain' (treating multiple delimiters as one).
|
| | 56 | % strsplit(str, {' ', 'ain'})
|
| | 57 | % % {'The', 'r', 'in', 'Sp', 'stays',
|
| | 58 | % % 'm', 'ly', 'in', 'the', 'pl', '.'}
|
| | 59 | %
|
| | 60 | % % Split on all whitespace and on 'ain', and treat multiple
|
| | 61 | % % delimiters separately.
|
| | 62 | % strsplit(str, {'\s', 'ain'}, 'CollapseDelimiters', false, ...
|
| | 63 | % 'DelimiterType', 'RegularExpression')
|
| | 64 | % % {'The', 'r', '', 'in', 'Sp', '', 'stays',
|
| | 65 | % % 'm', 'ly', 'in', 'the', 'pl', '.'}
|
| | 66 | %
|
| | 67 | % See also SPLIT, JOIN, STRJOIN, REGEXP, CONTAINS, COUNT, EXTRACTBETWEEN,
|
| | 68 | % STRFIND
|
| | 69 |
|
| | 70 | % Copyright 2012-2016 The MathWorks, Inc.
|
| | 71 |
|
| | 72 | % Initialize default values.
|
< 0.001 | 2 | 73 | collapseDelimiters = true;
|
< 0.001 | 2 | 74 | delimiterType = 'Simple';
|
| | 75 |
|
| | 76 | % Check input arguments.
|
< 0.001 | 2 | 77 | if nargin < 1
|
| | 78 | narginchk(1, Inf);
|
< 0.001 | 2 | 79 | elseif ~ischar(str) && ~(isstring(str) && isscalar(str))
|
| | 80 | error(message('MATLAB:strsplit:InvalidStringType'));
|
< 0.001 | 2 | 81 | end
|
< 0.001 | 2 | 82 | if nargin < 2
|
< 0.001 | 2 | 83 | delimiterType = 'RegularExpression';
|
< 0.001 | 2 | 84 | aDelim = {'\s'};
|
| | 85 | elseif ischar(aDelim)
|
| | 86 | aDelim = {aDelim};
|
| | 87 | elseif isstring(aDelim)
|
| | 88 | aDelim(ismissing(aDelim)) = [];
|
| | 89 | aDelim = cellstr(aDelim);
|
| | 90 | elseif ~iscellstr(aDelim)
|
| | 91 | error(message('MATLAB:strsplit:InvalidDelimiterType'));
|
< 0.001 | 2 | 92 | end
|
< 0.001 | 2 | 93 | if nargin > 2
|
| | 94 | funcName = mfilename;
|
| | 95 | p = inputParser;
|
| | 96 | p.FunctionName = funcName;
|
| | 97 | p.addParameter('CollapseDelimiters', collapseDelimiters);
|
| | 98 | p.addParameter('DelimiterType', delimiterType);
|
| | 99 | p.parse(varargin{:});
|
| | 100 | collapseDelimiters = verifyScalarLogical(p.Results.CollapseDelimiters, ...
|
| | 101 | funcName, 'CollapseDelimiters');
|
| | 102 | delimiterType = validatestring(p.Results.DelimiterType, ...
|
| | 103 | {'RegularExpression', 'Simple'}, funcName, 'DelimiterType');
|
< 0.001 | 2 | 104 | end
|
| | 105 |
|
| | 106 | % Handle DelimiterType.
|
< 0.001 | 2 | 107 | if strcmp(delimiterType, 'Simple')
|
| | 108 | % Handle escape sequences and translate.
|
| | 109 | aDelim = strescape(aDelim);
|
| | 110 | aDelim = regexptranslate('escape', aDelim);
|
< 0.001 | 2 | 111 | else
|
| | 112 | % Check delimiter for regexp warnings.
|
< 0.001 | 2 | 113 | regexp('', aDelim, 'warnings');
|
< 0.001 | 2 | 114 | end
|
| | 115 |
|
| | 116 | % Handle multiple delimiters.
|
0.003 | 2 | 117 | aDelim = strjoin(aDelim, '|');
|
| | 118 |
|
| | 119 | % Handle CollapseDelimiters.
|
< 0.001 | 2 | 120 | if collapseDelimiters
|
< 0.001 | 2 | 121 | aDelim = ['(?:', aDelim, ')+'];
|
< 0.001 | 2 | 122 | end
|
| | 123 |
|
| | 124 | % Split.
|
< 0.001 | 2 | 125 | [c, matches] = regexp(str, aDelim, 'split', 'match');
|
| | 126 |
|
< 0.001 | 2 | 127 | end
|
Other subfunctions in this file are not included in this listing.